tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > JSON LIB > JSON Array to List

JSON Array to List 

JSON-lib is a java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format. Get the latest binaries from sourceforge http://json-lib.sourceforge.net/. This requires the libraries ( json-lib-2.4-jdk15.jar, xom-1.2.7.jar, ezmorph.jar commons-lang.jar, commons-collections.jar, commons-beanutils-1.7.jar and commons-logging-1.1.1.jar) to be in classpath. The following example shows converting a JSON Array to List.

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

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;

public class JsonArray2List {

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

    JSONArray jsonArray = new JSONArray()
            .element("one")
            .element(new Integer(2))
            .element(new Long(3))
            .element(new Double(4.26))
            .element(true)
            .element(new char [] { 'A''B''C' });
    
    String jsonStr = jsonArray.toString(2)//pretty print with indent
    System.out.println(jsonStr)
    
    List list = (ListJSONSerializer.toJava(jsonArray);
    System.out.println(list);
  }

}
   

It gives the following output,
[
  "one",
  2,
  3,
  4.26,
  true,
    [
    "A",
    "B",
    "C"
  ]
]

[one, 2, 3, 4.26, true, [A, B, C]]




 
  


  
bl  br