Home >Java >javaTutorial >How to deserialize Java objects from Reader stream in Java using flexjson?

How to deserialize Java objects from Reader stream in Java using flexjson?

王林
王林forward
2023-09-10 22:57:041051browse

How to deserialize Java objects from Reader stream in Java using flexjson?

Flexjson is a lightweight library for serializing and deserializing Java objects into JSON format. JSON format. We can deserialize Java objects from a Reader stream using the deserialize() method of the JSONDeserializer class, which uses an instance of the Reader class as JSON input.

Syntax

public T deserialize(Reader input)

Example

import java.io.*;
import flexjson.JSONDeserializer;
public class JSONDeserializeReaderTest {
   public static void main(String[] args) {
      JSONDeserializer<Student> deserializer = new JSONDeserializer<Student>();
      String jsonStr =
                       "{" +
                        "\"firstName\": \"Adithya\"," +
                        "\"lastName\": \"Sai\"," +
                        "\"age\": 25," +
                        "\"address\": \"Hyderabad\"" +
                        "\"class\": \"Student\"" +
                       "}";
      Student student = deserializer.deserialize(new StringReader(jsonStr));
      System.out.println(student);
   }
}
// Student class
class Student {
   private String firstName;
   private String lastName;
   private int age;
   private String address;
   public Student() {}
   public Student(String firstName, String lastName, int age, String address) {
      super();
      this.firstName = firstName;
      this.lastName = lastName;
      this.age = age;
      this.address = address;
   }
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getAddress() {
      return address;
   }
   public void setAddress(String address) {
      this.address = address;
   }
   public String toString() {
      return "Student[ " +
      "firstName = " + firstName +
      ", lastName = " + lastName +
      ", age = " + age +
      ", address = " + address +
      " ]";
   }
}

Output

Student[ firstName = Adithya, lastName = Sai, age = 25, address = Hyderabad ]

The above is the detailed content of How to deserialize Java objects from Reader stream in Java using flexjson?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete