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