我有 JSON,我想将第一个对象的数据合并到第二个对象中
{
"ViewId": {
"56": {
"ViewId": "56",
"Name": "hi",
"param": "value"
},
"88": {
"ViewId": "88",
"Name": "hi2",
"param": "value2"
}
},
"que": [
{
"RId": "123",
"ViewId": "88",
"Count": 0
},
{
"RId": "456",
"ViewId": "56",
"Count": 0
}
]
}
基本上我正在制作 ArrayList,如何将 ViewId 数据添加到队列中。 我想通过以下方式合并 JSON:
{
"que": [
{
"RId": "123",
"ViewId": "88",
"Name": "hi2",
"param": "value2",
"Count": 0
},
{
"RId": "456",
"ViewId": "56",
"Name": "hi",
"param": "value",
"Count": 0
}
]
}
最佳答案
JSONObject ViewIdJsnObject = new JSONObject(); //replace new JSONObject() with ViewId Json Object here
JSONArray queArray = new JSONArray();//replace new JSONArray() with actual json array;
//Traverse through all que objects in array
if(queArray != null && queArray.length() > 0){
for(int i=0; i<queArray.length(); i++){
try {
JSONObject queObj = queArray.getJSONObject(i);
String queViewId = queObj.getString("ViewId"); //ViewId of que object at position i
JSONObject viewIdObj = ViewIdJsnObject.getJSONObject(queViewId); //get json object against ViewId
if(viewIdObj != null) {
//Now add these value to que object at position i
String name = viewIdObj.getString("Name");
String param = viewIdObj.getString("param");
queObj.put("Name", name);
queObj.put("param", param);
}
} catch (JSONException jse) {
jse.printStackTrace();
}
}
}
//Now que array contains final merged data, convert it to ArrayList<Your_model>.
关于android - 合并 json 并制作 arraylist<object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36834148/