【问题标题】:Android - Parse a Nested JSON array with VolleyAndroid - 使用 Volley 解析嵌套的 JSON 数组
【发布时间】:2020-07-25 08:27:20
【问题描述】:

我对 JSON 解析非常陌生,每天都在学习。

我必须解析一个特定的 JSON 响应,但我发现没有运气。

我正在使用 Volley 来解析请求。

这是我的回复:

{
    "error": false,
    "message": "Favourties fetched  successfully",
    "code": 200,
    "data": [
        {
            "id": "5f1980f8c42e1f60854c57e4",
            "type": 1,
            "status": 1,
            "favourite": {
                "_id": "5f118057f44ebd1cead089db",
                "firstName": "Bilal",
                "lastName": "Khan",
                "businessName": "Master Paint",
                "image": "https://welist-assets.s3.us-west-2.amazonaws.com/profile_images/1592383845941-default_avatar.png"
            },
            "createdAt": "2020-07-23T12:22:16.731Z",
            "updatedAt": "2020-07-23T12:22:16.731Z"
        },
        {
            "id": "5f198084c42e1f60854c57e2",
            "type": 3,
            "status": 1,
            "favourite": {
                "images": [],
                "_id": "5f12d5345478a53584eca98b",
                "name": "Water Paint7"
            },
            "createdAt": "2020-07-23T12:20:20.680Z",
            "updatedAt": "2020-07-23T12:20:20.680Z"
        }
    ]
}

我能够获取包含 2 个对象的数组,但无法获取嵌套收藏夹中的数据。

try {
                            Log.d("Onresponse", response);
                            //converting the string to json array object
                            JSONObject obj = new JSONObject(response);
                            JSONArray array = obj.getJSONArray("data");
                            //traversing through all the object

                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject favorites = array.getJSONObject(i);
                                Log.d("lenght", favorites.getString("status"));
                                //adding the product to product list
                                favoriteList.add(new FavoriteVendorsModel(
                                        favorites.getString("firstName"),
                                        favorites.getString("lastName"),
                                        favorites.getString("businessName"),
                                        favorites.getString("image")
                                ));
                            }

这是我的方法。

【问题讨论】:

  • 不推荐使用 volley 了,改造更容易,每个项目都使用它。操作 Json 对象也很痛苦,我知道没有项目这样做。不要粗鲁,但如果我必须对其进行代码审查,我会不假思索地拒绝它。看看 gson、Jackson 或 moshi,它们在改造或独立于它时工作得非常好。

标签: android json android-volley


【解决方案1】:

您已跳过一级嵌套。致电array.getJSONObject(i),您将获得:

{
    "id": "5f1980f8c42e1f60854c57e4",
    "type": 1,
    "status": 1,
    "favourite": {
        "_id": "5f118057f44ebd1cead089db",
        "firstName": "Bilal",
        "lastName": "Khan",
        "businessName": "Master Paint",
        "image": "https://welist-assets.s3.us-west-2.amazonaws.com/profile_images/1592383845941-default_avatar.png"
    },
    "createdAt": "2020-07-23T12:22:16.731Z",
    "updatedAt": "2020-07-23T12:22:16.731Z"
}

你正试图直接调用该对象的下一个 getString 方法:

favorites.getString("firstName");
favorites.getString("lastName");
favorites.getString("businessName");
favorites.getString("image");

实际上你必须先获取名为favourite的JSONObject,然后在上面调用这些getString方法:

JSONObject favorite = favorites.getJSONObject("favourite");

favorite.getString("firstName");
favorite.getString("lastName");
favorite.getString("businessName");
favorite.getString("image");

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多