{
"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
This is an example of 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"));
Results
======before=====
size: 1
val: 123
======after======
size: 1
val: 234
PHP中文网2017-04-18 09:14:51
What json library are you using? Just change it using the corresponding method
高洛峰2017-04-18 09:14:51
I assume you are using com.googlecode.json-simple
Kuba (the information you provided is really too little, I don’t know where to start)
Then, I assume you want to modify shops
里第一项的shopName
, and the code is written like this:
JSONObject res = (JSONObject) obj.get("result");
JSONArray arr = (JSONArray)res.get("shops");
arr.set(0, "shopName: fuck");
System.out.println(obj);//检查结果
If you want to add something to shops
, you can:
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);//检查结果
迷茫2017-04-18 09:14:51
According to the Google json or Alibaba fastjson you use, use the methods they provide to modify it.