如何用android中的gson解析嵌套的jsonobject?

时间:2017-01-31 07:22:17

标签: android json gson

如何用android中的gson解析嵌套的jsonobject?

错误:catch:org.json.JSONException:6没有值

  

我知道没有位置6的原因但我不知道如何将json响应转换为其他方式

     

我有嵌套jsonobjects的json响应我想用gson解析它   我试图解析,但抛出错误..

以下是回复

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

以下代码

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

2 个答案:

答案 0 :(得分:0)

最好是拥有数据数组而不是Json Object,但是如果你必须循环遍历对象内的对象,那么使用迭代器而不是循环。

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

答案 1 :(得分:0)

你可以使用gson为json解析创建适当的POJO。通过在线:

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

转到上面的URL并粘贴您的json响应,然后选择

来源类型:json

注释样式:gson

但是在您的响应中,数据编号将作为json对象出现,如果它始终相同,那么请给出您的解决方案。如果没有,那么你必须将所有可能的数字与他们的getter和setter方法一起添加到pojo中。