2016-04-25 12 views
1

私は、どのように私はQUEに中のviewIdデータを、私は2番目のオブジェクト私はArrayListのを作っています。このことから、基本的に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 
     } 
    ] 
} 

に第一オブジェクトからのデータを結合したいJSON追加することができている作ります。 は、私は次のようにJSONをマージしたい:クラス

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": [ 
     { 
      "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の第1要素のように、最初の要素にマージする必要がありますか? – 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

別のクラスが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これは動作します。すみません、私は怠け者でした。上記の "que"( "ViewId": "56":{....}、.......}などの他のjsonフィールドについては、 – mubeen

関連する問題