2016-04-25 108 views
1

我已经JSON,我想从第一个对象的数据合并到第二个对象结合JSON,使的ArrayList <object>

{ 
    "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 
     } 
    ] 
} 
+0

目前尚不清楚你想要什么。请尝试更正确地提出您的问题。请使用逗号和大写字母。 –

+0

你可以运行一个循环并检查viewId数组中的相应JSONObject并修改它。 –

+0

你想将viewId的元素合并到que中吗?像viewId的第一个元素应该合并到que的第一个元素中,对吧? – Pehlaj

回答

1
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>. 
+0

queObj没有添加它将始终重叠数据 – andro

+0

您应该直接将它添加到您的数组列表中。首先添加que对象,然后添加相应的名称和参数值 – Pehlaj

+1

JSONArray finalValue = new JSONArray(); ,然后在for循环中添加到finalValue.put(queObj)。 finalValue将具有所有数据 – andro

1

使类

public class Data { 
    int id; 
    List<Que> que = new ArrayList<Que>(); 


    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public List<Que> getQue() { 
     return que; 
    } 
    public void setQue(List<Que> que) { 
     this.que = que; 
    } 

} 

请另一类称为Que

public class Que { 
    int RId; 
    int ViewId; 
    int Count; 


    public int getrId() { 
     return RId; 
    } 
    public void setrId(int rId) { 
     this.RId = rId; 
    } 
    public int getViewId() { 
     return ViewId; 
    } 
    public void setViewId(int viewId) { 
     this.ViewId = viewId; 
    } 
    public int getCount() { 
     return Count; 
    } 
    public void setCount(int count) { 
     this.Count = count; 
    } 

} 

使用使用它gson

Gson gson = new Gson(); 
     Data data = gson.fromJson(json, Data.class); 
     List<Que> queList = data.getQue(); 
     for(Que que : queList){ 
      System.out.println("This is R ID" +que.RId); 
      System.out.println("This is View ID" +que.ViewId); 
      System.out.println("This is Count" +que.Count); 

确保您的json属性名称与java实例参数匹配。

+0

@andro这将工作。我很抱歉,我很懒。你必须找出其他的json字段,比如上面的“que”,它是“ViewId”:“56”:{....},.......}, – mubeen

相关问题