Unable to parse JSON in Android [closed]

折月煮酒 提交于 2019-12-25 18:48:40

问题


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"
            }
        ]
    }
}

回答1:


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

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



回答2:


I like to use the GSON library: http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

It's a JSON-parsing library by Google.




回答3:


OK, step by step:

String json = "{\"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\"}]}}";

try
{
    JSONObject o = new JSONObject(json);
    JSONObject result = o.getJSONObject("Result");
    JSONArray data = result.getJSONArray("Data");

    for (int i = 0; i < data.length(); i++)
    {
        JSONObject entry = data.getJSONObject(i);
        String name = entry.getString("Name");
        Log.d("name key", name);
    }   
}
catch (JSONException e)
{
    e.printStackTrace();
}

Json is hardcoded, so I had to escape it. This code gets result object and then data array. A loop goes through an array and gets a value of Name.

I got in LogCat: ABC CDE

Note that you should surround it with try-catch or add throws to a method.



来源:https://stackoverflow.com/questions/12860086/unable-to-parse-json-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!