2017-09-07 4 views
0

Ich bekomme Daten von JSON.In meinem JSON gibt es 2 Array.Wie zu speichern Json Array In ArrayList

  1. Haupt Array
  2. Unter Hauptarray gibt es mehr Objekt und unter diesem Objekt Ich erhalte eine andere Anordnung.

Das Problem ist, lesen, wenn Erstes Objekt dann geht sein zu ParseLiveComment (GET Below Coderuf sehen) und lesen Sie alle Objekt dann alle Kommentar und Anzeige in Liste zurückzukehren

Aber ich möchte, wenn erstes Objekt lesen dann geht seine zu ParseLiveComment (GET Below Coderuf sehen) und dann die erste Objekt Kommentar Daten hinzufügen, und fügen Sie Arraylist kommentieren, und so weiter

Das ist meine Json

{ 
    "status": 1, 
    "product": [ 
     { 
      "id": 1, 
      "product_name_en": "testProduct", 
      "product_name_ar": "testProduct", 
      "product_description_en": "This is test product", 
      "product_description_ar": "This is test product", 
      "link": [ 
       "http://kartpay.biz/api/v1/file/product/WMZv45E8UCWhIG95ammJSF9GePbz6CjgfskkRPK9.jpg" 
      ], 
      "starting_price": "1000.000", 
      "sub_category_id": "1", 
      "status": "A", 
      "user_id": "1", 
      "view_count": null, 
      "like_count": null, 
      "comment_count": 1, 
      "comment": [ 
       { 
        "product_id": "1", 
        "user_id": "1", 
        "comment": "Good and nice product" 
       } 
      ], 
      "first_name_en": null, 
      "first_name_ar": null, 
      "last_name_en": null, 
      "last_name_ar": null, 
      "created_at": "Sep 4 2017 8:11 AM", 
      "updated_at": "Sep 4 2017 8:11 AM" 
     }, 
     { 
      "id": 2, 
      "product_name_en": "testProducts", 
      "product_name_ar": "testProducts", 
      "product_description_en": "This is test products", 
      "product_description_ar": "This is test", 
      "link": [ 
       "http://kartpay.biz/api/v1/file/product/FjtsAmhCxINBhleAl2sTtKcZehHokRaoO1T2aYW4.jpg", 
       "http://kartpay.biz/api/v1/file/product/LNG9gTlvnHzOpi3Hb5WNoWR8YN1oXuHwOOA6Nbqn.jpg" 
      ], 
      "starting_price": "1000.000", 
      "sub_category_id": "1", 
      "status": "A", 
      "user_id": "1", 
      "view_count": null, 
      "like_count": null, 
      "comment_count": 0, 
      "comment": [], 
      "first_name_en": null, 
      "first_name_ar": null, 
      "last_name_en": null, 
      "last_name_ar": null, 
      "created_at": "Sep 4 2017 8:19 AM", 
      "updated_at": "Sep 4 2017 8:19 AM" 
     } 
] 
} 

Ich bin erfolgreich Haupt-Array in Array-Liste erhalten, aber ich will als diesen Kommentar Array in einer anderen Array Speicher-Liste pro

Das ist meine Pojo Klasse für beide Arraylist

public class CardData implements ECCardData<Comment> { 

    private List<Comment> listItems; 
@Override 
    public List<Comment> getListItems() { 
     return listItems; 
    } 

    public void setListItems(List<Comment> listItems) { 
     this.listItems = listItems; 
    } 
} 

Kommentar POJO

public class Comment { 

    private String commentText; 
    public String getCommentText() { 
     return commentText; 
    } 

    public void setCommentText(String commentText) { 
     this.commentText = commentText; 
    } 
} 

Das ist mein Parsing

public static ArrayList<CardData> PaeseLiveProducts(String response) throws JSONException { 
ArrayList<CardData> alUser = new ArrayList<>(); 

    JSONObject jsonRoot = new JSONObject(response); 

    JSONArray parentArray = jsonRoot.getJSONArray("product"); 
    for (int j = 0; j < parentArray.length(); j++) { 
     JSONObject finalObject = parentArray.getJSONObject(j); 
     CardData user = new CardData(); 

    int is= user.setId(finalObject.getInt("id")); 

     Comment comment = new Comment(); 

     JSONArray jsonArray=finalObject.getJSONArray("link"); 
     for (int i = 0; i < jsonArray.length(); i++) { 
      // Log.d("text", textArray.getString(i)); 
      user.setHeadBackgroundResource(jsonArray.getString(i)); 
     } 

     JSONArray jsonArray1 = finalObject.getJSONArray("comment"); 
     for (int k = 0; k < jsonArray1.length(); k++) { 
      // Log.d("text", textArray.getString(i)); 
      JSONObject jsonObject2 = jsonArray1.getJSONObject(k); 
      comment.setCommentText(jsonObject2.getString("comment")); 
     } 

     user.setListItems(PaeseLive(response)); 


     alUser.add(user); 


    } 
    return alUser; 
} 

public static ArrayList<Comment> PaeseLiveComment(String response) throws JSONException { 

    ArrayList<Comment> alUser = new ArrayList<>(); 
    JSONObject jsonRoot = new JSONObject(response); 
    JSONArray parentArray = jsonRoot.getJSONArray("product"); 

    for (int j = 0; j < parentArray.length(); j++) { 
     JSONObject finalObject = parentArray.getJSONObject(j); 
     Comment comment = new Comment(); 

     JSONArray jsonArray1 = finalObject.getJSONArray("comment"); 
     for (int k = 0; k < jsonArray1.length(); k++) { 
      // Log.d("text", textArray.getString(i)); 
      JSONObject jsonObject2 = jsonArray1.getJSONObject(k); 
      comment.setCommentText(jsonObject2.getString("comment")); 
     } 
     alUser.add(comment); 

    } 
    return alUser; 
    // return alUser; 
} 

Here in All comment List Display at a time , i want to show onlyfor particular product_id

+1

Haben Sie 'GSON' versucht? Ich denke, Gson wird bequemer sein als manuelles Parsen. –

+0

Parsing funktioniert perfekt, aber das Problem ist, wenn es das erste Objekt liest, dann liest es alle Kommentar-Array. Ich möchte, wenn es erste Hauptobjekt gelesen, dann liest es das Objekt Kommentar Array, wenn es zweite, dann lesen Sie 2. Objekt Kommentar Array und so weiter –

+0

Ihre Beschreibung ist ziemlich verwirrend, können Sie erklären, wenn Sie das erste Element dann bekommen, was Sie wollen genau. Soweit ich verstehe, möchten Sie das Array von Kommentaren nehmen und analysieren? – Umair

Antwort

0

Können Sie bitte Code unten versuchen? :

public static ArrayList<Comment> PaeseLiveComment(String response) throws JSONException { 

    ArrayList<Comment> alUser = new ArrayList<>(); 
    JSONObject jsonRoot = new JSONObject(response); 
    JSONArray parentArray = jsonRoot.getJSONArray("product"); 

    for (int j = 0; j < parentArray.length(); j++) { 
     JSONObject finalObject = parentArray.getJSONObject(j); 

     JSONArray jsonArray1 = finalObject.getJSONArray("comment"); 
     for (int k = 0; k < jsonArray1.length(); k++) { 
      // Log.d("text", textArray.getString(i)); 
      Comment comment = new Comment(); 
      JSONObject jsonObject2 = jsonArray1.getJSONObject(k); 
      comment.setCommentText(jsonObject2.getString("comment")); 
      alUser.add(comment); 
     } 

    } 
    return alUser; 
    // return alUser; 
}