2016-07-13 126 views
0

我想檢索related_ids數組的數據,但未能這樣做。我的代碼不分別給我每個數組的值。通過json檢索數組數據

{ 
    "title": "Prod" 
    "related_ids": [3] 
    0: 4323 
    1: 4321 
    2: 4317 
    "tags": [0] 
    } 

這是我的代碼。

JSONArray jsonArray = response.getJSONArray("showProductListResponse"); 

    for (int i = 0; i < jsonArray.length(); i++) { 
     try { 
      JSONObject obj = jsonArray.getJSONObject(i); 
      HomelList productListItems = new HomelList(); 

      productListItems.setName(obj.getString("title"));          

      try { 
       JSONArray relatedimages = obj.getJSONArray("related_ids"); 
       for (int j = 0; j < relatedimages.length(); j++) { 
        JSONObject relimagesObject = relatedimages.getJSONObject(j); 
        productListItems.setRelatedids(relimagesObject.getInt(relimagesObject.indexOf[j]); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      gavkidata.add(productListItems); 
      adapter.notifyDataSetChanged(); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

回答

0

您的JSON數據無效。

我想應該是這樣的

{ 
    "title": "Prod", 
    "related_ids": [ 
    4323, 
    4321, 
    4317 
    ], 
    "tags": [] 
} 

,或者related_ids應包含對象改變它像這樣

{ 
    "title": "Prod", 
    "related_ids": [ 
    {0: 4323}, 
    {1: 4321}, 
    {2: 4317} 
    ], 
    "tags": [] 
}