java - 如何修改JSONObject 的值
PHP中文网
PHP中文网 2017-04-18 09:12:51
0
4
934

{

"result": {
    "total": "3",
    "shops": [
        {
            "shopId": "8b615ce0",
            "shopName": "舒",
            "icon": 
        },
        {
            "shopId": "f6f83000a",
            "shopName": "二号",
            "icon": 
        },
    ]
},
"code": 0,
"message": "OK",
"text": "OK"

}
JSONObject 对象rt
想要修改“shops” 对应的值怎么修改??

PHP中文网
PHP中文网

认证0级讲师

reply all(4)
伊谢尔伦

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
Ty80

What json library are you using? Just change it using the corresponding method

小葫芦

I assume you are using com.googlecode.json-simpleKuba (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);//检查结果
迷茫

According to the Google json or Alibaba fastjson you use, use the methods they provide to modify it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!