tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > FLEX JSON > How to Deserialize JSON Stream to List

How to Deserialize JSON Stream to List 

Flexjson is a lightweight java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format. Get the latest binaries from https://sourceforge.net/projects/flexjson/. This requires the library flexjson-2.1.jar to be in classpath. The following example shows how to Deserialize list of Objects from an InputStream.

File Name  :  
/FLEX_JSON/config/student_list.json 
   
[
    {
        "age"2,
        "class""com.bethecoder.tutorials.flexjson.common.Student",
        "dob"1341343591640,
        "firstName""Sriram",
        "hobby""Singing",
        "lastName""Kasireddi"
    },
    {
        "age"29,
        "class""com.bethecoder.tutorials.flexjson.common.Student",
        "dob"1341343591640,
        "firstName""Sudhakar",
        "hobby""Painting",
        "lastName""Kasireddi"
    },
    {
        "age"29,
        "class""com.bethecoder.tutorials.flexjson.common.Student",
        "dob"1341343591640,
        "firstName""Anu",
        "hobby""Singing",
        "lastName""Kasireddi"
    }
]
   

File Name  :  
com/bethecoder/tutorials/flexjson/common/Student.java 
   
package com.bethecoder.tutorials.flexjson.common;

import java.util.Date;

public class Student {
  private String firstName;
  private String lastName;
  private int age;
  private String hobby;
  private Date dob;

  public Student() {}
  
  public Student(String firstName, String lastName, int age, String hobby,
      Date dob) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.hobby = hobby;
    this.dob = dob;
  }
  
  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 getHobby() {
    return hobby;
  }

  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  public Date getDob() {
    return dob;
  }
  public void setDob(Date dob) {
    this.dob = dob;
  }

  public String toString() {
    return "Student[ " +
      "firstName = " + firstName +
      ", lastName = " + lastName +
      ", age = " + age +
      ", hobby = " + hobby +
      ", dob = " + dob +
      " ]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/flexjson/tests/DeserializeListTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.flexjson.tests;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.bethecoder.tutorials.flexjson.common.Student;

import flexjson.JSONDeserializer;

public class DeserializeListTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    JSONDeserializer<List<Student>> deserializer = new JSONDeserializer<List<Student>>();
    InputStream input = DeserializeListTest.class
      .getClassLoader().getResourceAsStream("student_list.json");
    
    List<Student> students = deserializer.deserialize(new InputStreamReader(input));
    
    for (Student std : students) {
      System.out.println(std);
    }
    
  }

}
   

It gives the following output,
Student[ firstName = Sriram, lastName = Kasireddi, age = 2, 
	hobby = Singing, dob = Wed Jul 04 00:56:31 IST 2012 ]
Student[ firstName = Sudhakar, lastName = Kasireddi, age = 29, 
	hobby = Painting, dob = Wed Jul 04 00:56:31 IST 2012 ]
Student[ firstName = Anu, lastName = Kasireddi, age = 29, 
	hobby = Singing, dob = Wed Jul 04 00:56:31 IST 2012 ]



 
  


  
bl  br