2016-04-25 28 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

相關問題