It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                7年前关闭。
            
        

我有一个来自服务器的JSON,如下所示

   {
    "XXXX": {
        "type": "RSS",
        "value": ""
    },
    "YYYY": {
        "type": "String",
        "value": ""
    },
    "ZZZZ": {
        "type": "String",
        "value": ""
    }
}


现在,我需要在所有XXXX,YYYY和ZZZZ的字段中添加String值。

我正在使用eclipse,我需要在XXXX和YYYY和ZZZZ中更改“值”的值,并且需要添加字段

{
    "MMMM": {
        "type": "Image",
        "value": "a7e8bec0-87ed-11e2-aa2e-52540025ab96_2_1362746556"
    }
}


经过ZZZZ。请让我知道该怎么做。

最佳答案

尝试这个

String jsonstring="{
    "XXXX": {
        "type": "RSS",
        "value": ""
    },
    "YYYY": {
        "type": "String",
        "value": ""
    },
    "ZZZZ": {
        "type": "String",
        "value": ""
    }
}";

JSONObject object=new JSONObject(jsonstring);
JSONObject childobject=object.getJSONObject("XXXX");

JSONObject modifiedjson=new JSONObject();
modifiedjson.put("type",childobject.get("type"));
modifiedjson.put("value","newvalue");  // Add new value of XXXX here

//

JSONObject mmjson=new JSONObject();
mmjson.put("type","image");
mmjson.put("value","a7e8bec0-87ed-11e2-aa2e-52540025ab96_2_1362746556");  // Add new value of MMM here

JSONObject newjson=new JSONObject();
newjson.put("MMMM",mmjson.toString());
newjson.put("XXXX",modifiedjson.toString());
newjson.put("YYYY",object.get("YYYY"));
newjson.put("ZZZZ",object.get("ZZZZ"));

09-28 08:36