from: http://code.google.com/p/json-simple/wiki/DecodingExamples
Example 1 - Convenient way: Use JSONValue
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=JSONValue.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=JSONValue.parse(s);
System.out.println(obj);
s="[5,]";
obj=JSONValue.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=JSONValue.parse(s);
System.out.println(obj);
JSONObject is a java.util.Map and JSONArray is a java.util.List, so you can access them
with standard operations of Map or List. Please refer Mapping Between JSON and Java Entities for more information on entity mapping while parsing.
本文展示了如何使用JSONSimple库将JSON字符串解析为Java对象,并通过标准操作访问映射和列表。提供了实例代码,包括JSON数组和对象的解析及访问。
529

被折叠的 条评论
为什么被折叠?



