Example: Encode complex LinkedHashMap data structure with JSON.simple

1,419 views
Skip to first unread message

Yidong

unread,
Mar 22, 2009, 12:07:37 AM3/22/09
to json-simple
Code:
ArrayList l1 = new ArrayList();
l1.add(1);
l1.add(2);
l1.add(3);

ArrayList l2 = new ArrayList();
l2.add(1);
l2.add(2);
l2.add(3);

ArrayList l3 = new ArrayList();
l3.add(1);
l3.add(2);
l3.add(3);

LinkedHashMap map1 = new LinkedHashMap();
map1.put("L1-1", l1);
map1.put("L1-2", l2);
map1.put("L1-3", l3);

LinkedHashMap map2 = new LinkedHashMap();
map2.put("L1-1", l1);
map2.put("L1-2", l2);
map2.put("L1-3", l3);

LinkedHashMap map3 = new LinkedHashMap();
map3.put("L1-1", l1);
map3.put("L1-2", l2);
map3.put("L1-3", l3);

LinkedHashMap map21 = new LinkedHashMap();
map21.put("L2-1", map1);
map21.put("L2-2", map2);
map21.put("L2-3", map3);

LinkedHashMap map22 = new LinkedHashMap();
map22.put("L2-1", map1);
map22.put("L2-2", map2);
map22.put("L2-3", map3);

LinkedHashMap map23 = new LinkedHashMap();
map23.put("L2-1", map1);
map23.put("L2-2", map2);
map23.put("L2-3", map3);

LinkedHashMap map31 = new LinkedHashMap();
map31.put("L3-1", map21);
map31.put("L3-2", map22);
map31.put("L3-3", map23);

String s = JSONValue.toJSONString(map31);
System.out.println(s);

Result:
{"L3-1":{"L2-1":{"L1-1":[1,2,3],"L1-2":[1,2,3],"L1-3":[1,2,3]},"L2-2":
{"L1-1":[1,2,3],"L1-2":[1,2,3],"L1-3":[1,2,3]},"L2-3":{"L1-1":
[1,2,3],"L1-2":[1,2,3],"L1-3":[1,2,3]}},"L3-2":{"L2-1":{"L1-1":
[1,2,3],"L1-2":[1,2,3],"L1-3":[1,2,3]},"L2-2":{"L1-1":[1,2,3],"L1-2":
[1,2,3],"L1-3":[1,2,3]},"L2-3":{"L1-1":[1,2,3],"L1-2":[1,2,3],"L1-3":
[1,2,3]}},"L3-3":{"L2-1":{"L1-1":[1,2,3],"L1-2":[1,2,3],"L1-3":
[1,2,3]},"L2-2":{"L1-1":[1,2,3],"L1-2":[1,2,3],"L1-3":[1,2,3]},"L2-3":
{"L1-1":[1,2,3],"L1-2":[1,2,3],"L1-3":[1,2,3]}}}
Reply all
Reply to author
Forward
0 new messages