0

how to parse nested jsonobject with gson in android?

error:catch: org.json.JSONException: No value for 6

i know the reason there no position 6 but i dont get it how to convert json response to other way

i have json response with nested jsonobjects i wan to parse it with gson i m trying to parse but throws errors ..

below is the response

{
"success": true,
  "msg": "ok",
  "updates": {
    "data": {
      "0": {
        "noti_type": "liked_post",
        "noti_time": "2017-01-30 13:22:10",
        "album_id": "",
        "album_name": "my first v...",
        "description": "",
        "user_id": "",
        "id": "1598",
        "actor_profile_image": "",
        "actor_username": "altaf_shaikh_official",
        "ago": "17 hours ago",
        "link": ",
        "message": "has liked a file in your album\"my first very big filke name is wow\"",
        "image": "",
        "file_id": "16537"
      },
      "1": {
        "noti_type": "invite_to_album",
        "noti_time": "2017-01-27 07:31:26",
        "album_id": "16360",
        "album_name": "...",
        "description": "",
        "user_id": "810",
        "id": "1546",
        "actor_profile_image": "",
        "actor_username": "gggg",
        "ago": "27 January, 2017",
        "link": "",
        "message": "has invited you to an album \"dreqwrqwerqw\"",
        "image": ""
      },
      "2": {
        "noti_type": "friend_request_accepted",
        "noti_time": "2017-01-26 18:34:54",
        "album_id": null,
        "album_name": "",
        "description": "",
        "user_id": null,
        "id": "1538",
        "actor_profile_image": "",
        "actor_username": "Kamran11",
        "ago": "26 January, 2017",
        "link": "",
        "message": "has accepted your friend request.",
        "image": ""
      },
      "3": {
        "noti_type": "invite_to_album",
        "noti_time": "2017-01-26 08:02:41",
        "album_id": "16281",
        "album_name": "",
        "description": "my first album jbxdhahd jsjqhjqw",
        "user_id": "769",
        "id": "1515",
        "actor_profile_image": "",
        "actor_username": "",
        "ago": "26 January, 2017",
        "link": "",
        "message": "has invited you to an album \"asma\"",
        "image": ""
      },
      "4": {
        "noti_type": "invite_to_album",
        "noti_time": "2017-01-26 06:16:34",
        "album_id": "16145",
        "album_name": "",
        "description": "...",
        "user_id": "",
        "id": "1487",
        "actor_profile_image": "",
        "actor_username": "",
        "ago": "26 January, 2017",
        "link": "",
        "message": "has invited you to an album \"dhshsh\"",
        "image": ""
      },
      "5": {
        "noti_type": "invite_to_album",
        "noti_time": "2017-01-25 09:37:48",
        "album_id": "16280",
        "album_name": "0",
        "description": "",
        "user_id": "769",
        "id": "1462",
        "actor_profile_image": "",
        "actor_username": "",
        "ago": "25 January, 2017",
        "link": "",
        "message": "has invited you to an album \"0\"",
        "image": ""
      },
      "9": {
        "noti_type": "friend_request_accepted",
        "noti_time": "2017-01-21 09:48:36",
        "album_id": null,
        "album_name": "",
        "description": "",
        "user_id": null,
        "id": "1239",
        "actor_profile_image": "",
        "actor_username": "",
        "ago": "21 January, 2017",
        "link": "",
        "message": "has accepted your friend request.",
        "image": ""
      }
    }
  }
}

code below

    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject jsonObject) {
        super.onSuccess(statusCode, headers, jsonObject);
          JSONObject createdtrs_array = null;
        if (dialog.isShowing())
            dialog.dismiss();
        try {
            if (jsonObject.has("success") && jsonObject.getString("success").equals("true")) {
                JSONObject sync_reponse = jsonObject.getJSONObject("updates");
                createdtrs_array = sync_reponse.getJSONObject("data");
                Log.e("array", createdtrs_array.toString());
               /* List<NotificationData> list = gson.fromJson(createdtrs_array.toString(),
                        new TypeToken<ArrayList<NotificationData>>() {
                        }.getType());*/
                List<NotificationData> list = new ArrayList<>();
                for (int i = 0; i < createdtrs_array.length(); i++) {
                    //NotificationData data = new NotificationData();
                    JSONObject jsonObject1 = createdtrs_array.getJSONObject(String.valueOf(i));
                    NotificationData data = gson.fromJson(jsonObject1.toString(), NotificationData.class);
                    list.add(data);
                   /* String s = createdtrs_array.getJSONObject(String.valueOf(i)).toString();
                    NotificationData data = gson.fromJson(s, NotificationData.class);
                    list.add(data);*/
                    Log.e("data", data.getId() + " " + data.getNoti_type());
                }
                if (setOnNotificationSyncComplete != null) {
                    setOnNotificationSyncComplete.onSuccess(list);
                }

            } else {
                setOnNotificationSyncComplete.onFailure(jsonObject.getString("msg"));
            }
            ResponseCode = true;
        } catch (JSONException e) {
            Log.e("catch", e.toString());
        }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        super.onFailure(statusCode, headers, responseString, throwable);
        Log.e("Data", responseString);
    }
});
Community
  • 1
  • 1
  • Just generate a POJO object that represents a single json object with everything you need from it and then you can easily use it to parse your string.- jsonschema2pojo.org – Eenvincible Jan 31 '17 at 07:25
  • it's a valid json but if you change the format little bit then it will be easier for you. `data` is a `JSONObject if you change that to `JSONArray` then it will be easier for you to parse. – Kaushik Jan 31 '17 at 07:28
  • plase exaplain it by code if u can –  Jan 31 '17 at 07:28
  • @Kaushik i cant that a problem .. and i also like to get it as an array but as an front end developer can't change –  Jan 31 '17 at 07:29
  • http://stackoverflow.com/questions/26837541/how-to-get-particular-values-from-json-response-by-using-android-code/26838502#26838502 – Kaushik Jan 31 '17 at 07:42
  • [using gson lib](http://stackoverflow.com/questions/6796662/using-gson-to-parse-a-json-with-dynamic-key-and-value-in-android) – Kaushik Jan 31 '17 at 07:46

2 Answers2

0

it is better to have array of data rather than Json Object, but still if you have to loop through objects inside an object, use iterator not for loop.

Iterator iterator = createdtrs_array.keys();
   while(iterator.hasNext()){
    String key = (String)iterator.next();
    JSONObject jsonObject1 = createdtrs_array.getJSONObject(key);
                    NotificationData data = gson.fromJson(jsonObject1.toString(), NotificationData.class);
                    list.add(data);
                   /* String s = createdtrs_array.getJSONObject(String.valueOf(i)).toString();
                    NotificationData data = gson.fromJson(s, NotificationData.class);
                    list.add(data);*/
                    Log.e("data", data.getId() + " " + data.getNoti_type());
    }
Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25
0

you can create proper POJO for json parsing using gson . through online :

[http://www.jsonschema2pojo.org/]

Go to above URL and paste your json response and then choose

Source type: json

Annotation style: gson

But in your response under data numbers are coming as json object if its same for all time then pojo crates from above link given your solution . if not then you must add all possible numbers with their getters and setters method into pojo.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43