2016-08-31 7 views
-3

JSONの解析に問題があります。私はPARTオブジェクトにそれを解析し、リストに追加しようとしたが、オブジェクトが解析されていないので、どこかで、私は間違いを犯していJSONレスポンスを解析してリストに追加します

{ 
   "data": { 
      "1": [ 
         { 
            "id": "2", 
            "name": "Szelki bezpieczeństwa", 
            "quantity": "1", 
            "note": null, 
            "picture_id": null, 
            "code": "CCCCCCCCCCCCCC" 
         }, 
         { 
            "id": "3", 
            "name": "Kalosze do brodzenia, wysokie lub biodrowe", 
            "quantity": "2", 
            "note": "Do wymiany", 
            "picture_id": null, 
            "code": "DDDDDDDDDDDDDD" 
         } 
      ], 
      "2": [ 
         { 
            "id": "1", 
            "name": "Spodnie dla pilarza z ochroną przed przecięciem, klasa min. 1 (wg PN-EN 381-5)", 
            "quantity": "2", 
            "note": "Uszkodzone", 
            "picture_id": null, 
            "code": "DAAD086F690E1C36" 
         } 
      ] 
   } 
} 

:私はこのようなWebサービスからの応答を持っています。

public void onResponse(String response) { 

       Log.e(TAG, "onResponse WHOLE: " + response); 
       try { 
        JSONObject responseJSONObject = new JSONObject(response); 
        String arrayString = responseJSONObject.getJSONArray("data").toString(); 
        JSONArray responseJSONArray = new JSONArray(arrayString); 
        Part tempCarEquipment; 
        int index = 1; 
        for (int i = 0; i < responseJSONArray.length(); i++,index++) { 
         JSONObject object = responseJSONArray.getJSONObject(index); 
         JSONArray response2 = object.getJSONArray(Integer.toString(index)); 
         String id = object.getJSONObject(Integer.toString(i)).getString("id"); 
         String name= object.getJSONObject(Integer.toString(i)).getString("name"); 
         String quantity= object.getJSONObject(Integer.toString(i)).getString("quantity"); 
         String picture_id= object.getJSONObject(Integer.toString(i)).getString("picture_id"); 
         String code= object.getJSONObject(Integer.toString(i)).getString("code"); 
         tempCarEquipment = new Part(name,quantity,"",picture_id,code,id,"",0); 
         wholeList.add(tempCarEquipment); 
         Log.e(TAG, "wholeList: " + wholeList.size()); 

        } 

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

ご協力ありがとうございました!あなたはイテレータの応答

基本的な例この種のイテレータを使用する必要があります:)

+0

エラーは何ですか? – pooja

+0

私は何のエラーもありません。オブジェクトはリストに追加されません。 – Bartos

+0

はい私は:/おそらく構造に問題があります... – Bartos

答えて

0

JSONの解析

はこのlinkです。

0

現在の配列としてマップを解析しようとしている。

String arrayString = responseJSONObject.getJSONArray("data").toString(); 

代わりにこのような何かを試してみてください。また

List<JSONArray> objects = new ArrayList<>(); 
Iterator<String> keys = responseJSONObject.getJSONObject("data").keys(); 
while(keys.hasNext()) { 
    objects.add(responseJSONObject.getJSONObject("data").get(keys.next()); 
} 

// rest of your stuff 

、POJOを使用してJSONレスポンスを解析するGSONを使用することを検討してください。

関連する問題