Android JsonObject生成数组json

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:11   2076   0

方法一:

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}]

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP