JSON.simple - Primitive, Map, List Combination


Advertisements

Using JSONValue object, we can create a JSON which comprises of primitives, Map and List.

Following example illustrates the above concept.

Example

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONValue;

class JsonDemo {
   public static void main(String[] args) throws IOException {
      Map m1 = new LinkedHashMap(); 
      m1.put("k11","v11"); 
      m1.put("k12","v12"); 
      m1.put("k13", "v13");

      List l1 = new LinkedList();
      l1.add(m1);
      l1.add(new Integer(100));

      String jsonString = JSONValue.toJSONString(l1);
      System.out.println(jsonString);
   }
}

Output

[{"k11":"v11","k12":"v12","k13":"v13"},100]
Advertisements