方法一:
JSONArray array =new JSONArray();
JSONObject object =new JSONObject();
JSONObject object1 =new JSONObject();
JSONObject obj= new JSONObject();
try {
object.put("item1","value1");
object.put("age",12);
object.put("name","tom");
object1.put("item2","value2");
object1.put("age",12232);
object1.put("name","tom");
array.put(object);
array.put(object1);
obj.put("name",array);
System.out.println(obj.toString());
}catch (Exception e){
}
结果:{"name":[{"item1":"value1","name":"tom","age":12},{"item2":"value2","name":"tom","age":12232}]}
方法二:
JSONArray array1 =new JSONArray();
JSONObject object2 =new JSONObject();
JSONObject object3 =new JSONObject();
try {
object2.put("color","red");
object2.put("height",20);
object3.put("color","blue");
object3.put("height",1010);
array1.put(object2);
array1.put(object3);
System.out.println(array1.toString());
}catch (Exception e){
}
结果:[{"color":"red","height":20},{"color":"blue","height":1010}]