Heim > Fragen und Antworten > Hauptteil
{
"result": {
"total": "3",
"shops": [
{
"shopId": "8b615ce0",
"shopName": "舒",
"icon":
},
{
"shopId": "f6f83000a",
"shopName": "二号",
"icon":
},
]
},
"code": 0,
"message": "OK",
"text": "OK"
}
JSONObject 对象rt
想要修改“shops” 对应的值怎么修改??
伊谢尔伦2017-04-18 09:14:51
这是fastjson的例子
JSONObject json = JSON.parseObject("{val: 123}");
System.out.println("======before=====");
System.out.println("size: " + json.size());
System.out.println("val: " + json.get("val"));
json.put("val", 234); // 直接put相同的key
System.out.println("======after======");
System.out.println("size: " + json.size());
System.out.println("val: " + json.get("val"));
结果
======before=====
size: 1
val: 123
======after======
size: 1
val: 234
高洛峰2017-04-18 09:14:51
我假设你用的是com.googlecode.json-simple
库吧(你提供的信息真的太少了,不知道该从哪说起)
然后,我再假设你是要修改shops
里第一项的shopName
,代码这样写:
JSONObject res = (JSONObject) obj.get("result");
JSONArray arr = (JSONArray)res.get("shops");
arr.set(0, "shopName: fuck");
System.out.println(obj);//检查结果
如果你是想给shops
加一项内容,可以:
JSONObject res = (JSONObject) obj.get("result");
JSONArray arr = (JSONArray)res.get("shops");
JSONObject newShop = new JSONObject();
newShop.put("shopId", "asdfdsaf");
newShop.put("shopName", "毛驴");
newShop.put("icon", "");
arr.add(newShop);
System.out.println(obj);//检查结果