JSON.simple - Primitive, Object, Map, List


Advertisements

Using JSONValue object, we can create a JSON which comprises of primitives, Object, 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.JSONObject;
import org.json.simple.JSONValue;

class JsonDemo {
   public static void main(String[] args) throws IOException {
      JSONObject obj = new JSONObject();

      Map m1 = new LinkedHashMap(); 
      m1.put("k11","v11");
      m1.put("k12","v12");
      m1.put("k13", "v13");

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

      obj.put("m1", m1);
      obj.put("l1", l1);
      String jsonString = JSONValue.toJSONString(obj);
      System.out.println(jsonString);
   }
}

Output

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