无法在Android中解析JSON [英] Unable to parse JSON in Android

查看:140
本文介绍了无法在Android中解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析以下JSON响应.我无法提取JSON对象内部的JSONArray.我是JSON解析的新手,将不胜感激.

I want to parse the following JSON response. I couldn't extract the JSONArray which is inside the JSON object. I'm a novice to JSON parsing, any help would be appreciated.

{
    "Result": {
        "Data": [
            {
                "id": "1",
                "Name": "ABC",
                "release": "8",
                "cover_image": "august.png",
                "book_path": "Aug.pdf",
                "magazine_id": "1",
                "Publisher": "XYZ",
                "Language": "Astrological Magazine",
                "Country": "XYZ"
            },
            {
                "id": "2",
                "Name": "CDE",
                "release": "8",
                "cover_image": "august2012.png",
                "book_path": "aug.pdf",
                "magazine_id": "2",
                "Publisher": "XYZ",
                "Language": "Astrological Magizine",
                "Country": "XYZ"
            }
        ]
    }
}

推荐答案

实现JSON解析的基本代码如下:

Basic code for implementing JSON Parsing is like:

JsonObject objJSON = new JSONObject("YourJSONString");

JSONObject objMain = objJSON.getJSONObject("NameOfTheObject");
JSONArray objArray = objMain.getJSONArray("NameOfTheArray");  // Fetching array from the object

更新:

根据您的评论,我可以看到您尚未获取JSONArray数据",如果没有它,您将尝试获取特定对象的值/属性:

Update:

Based on your comment, i can see you haven't fetched JSONArray "Data", without it you are trying to fetch values/attributes of a particular object:

JSONObject jObj = jsonObj.getJSONfromURL(category_url); 
JSONObject menuObject = jObj.getJSONObject("Result"); String attributeId = menuObject.getString("Data");

String attributeId = menuObject.getString("Data");   // Wrong code

JSONArray objArray = menuObject.getJSONArray("Data"); // Right code

这篇关于无法在Android中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆