 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to deserialize a Java object from Reader Stream using flexjson in Java?
The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can deserialize a Java object from a Reader stream using the deserialize() method of JSONDeserializer class, it uses an instance of 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 ]
Advertisements
 