-3

I've got problem with parsing JSON. I've got response from webservice like this :

{
   "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"
         }
      ]
   }
}

I try to parse it into PART object and add it to List but somewhere I'm making mistake because object is not being parsed.

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();
                }
            }

Thanks in advance fo every help! :)

pooja
  • 393
  • 8
  • 16
Bartos
  • 1,007
  • 3
  • 15
  • 38

2 Answers2

0

JSON Parsing

You have to use the iterator for this kind of response

basic example of iterator is in this link.

Community
  • 1
  • 1
Amjad Khan
  • 1,309
  • 15
  • 32
0

You are trying to parse map as an array here:

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

Try something like this instead:

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

Alternatively, consider using GSON to parse JSON responses using POJOs.

Mr Quattro
  • 331
  • 1
  • 5