传入多层嵌套JSON

public static void main(String[] args) throws Exception {
HashMap<String, Object> map = new HashMap<>();
HashMap<String, Object> a = new HashMap<>();
HashMap<String, Object> b = new HashMap<>();
HashMap<String, Object> c1 = new HashMap<>();
HashMap<String, Object> c2 = new HashMap<>();
a.put("a1", "101");
a.put("a2", "102");
b.put("b1", "201");
b.put("b2", "202");
c1.put("c11", "301");
c1.put("c12", "302");
c2.put("c21", "303");
c2.put("c22", "304");
HashMap[] c = {c1, c2};
String d = "401";
map.put("a", a);
map.put("b", b);
map.put("c", c);
map.put("d", d);
JSONObject json = new JSONObject(map);
System.out.println("【JSON字符串】"+json.toString());
}

解析多层嵌套JSON

public static void main(String[] args) throws Exception {
String jsonStr = getJson();
JSONObject jsonObject = FastJson.getJson().parse(jsonStr, JSONObject.class);
JSONObject a = (JSONObject)jsonObject.get("a");
System.out.println("【a】" + a.toString());
JSONObject b = (JSONObject)jsonObject.get("b");
System.out.println("【b】" + b.toString());
JSONArray c = jsonObject.getJSONArray("c");
System.out.println("【c0】"+ c.get(0));
System.out.println("【c1】"+ c.get(1));
String d = jsonObject.getString("d");
System.out.println("【d】"+ d);
}
public static String getJson(){
HashMap<String, Object> map = new HashMap<>();
HashMap<String, Object> a = new HashMap<>();
HashMap<String, Object> b = new HashMap<>();
HashMap<String, Object> c1 = new HashMap<>();
HashMap<String, Object> c2 = new HashMap<>();
a.put("a1", "101");
a.put("a2", "102");
b.put("b1", "201");
b.put("b2", "202");
c1.put("c11", "301");
c1.put("c12", "302");
c2.put("c21", "303");
c2.put("c22", "304");
HashMap[] c = {c1, c2};
String d = "401";
map.put("a", a);
map.put("b", b);
map.put("c", c);
map.put("d", d);
JSONObject json = new JSONObject(map);
System.out.println("【JSON字符串】"+json.toString());
return json.toString();
}
