Reputation: 841
i'm trying to parse a json array "itemList":
{
"itemsCount": "1309",
"itemList": [
{ name: "xxx",
id: "01"
},
{ name: "yyy",
id: "02"
}
]
}
but i got " json exception:no value for " when i use the following code.
String json = jsonParser.makeHttpRequest(URL_ALBUMS, "GET", params);
Log.d("Albums JSON: ", "> " + json);
try {
jsonObject = new JSONObject().getJSONObject("itemList");
albums = jsonObject.getJSONArray("itemList");
if (albums != null) {
for (int i = 0; i < albums.length(); i++) {
JSONObject c = albums.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
albumsList.add(map);
}
}else{
Log.d("Albums: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
in the log i'm able to see the json values. i'm getting error in following lines.
jsonObject = new JSONObject().getJSONObject("itemList");
albums = jsonObject.getJSONArray("itemList");
i'm new to this .help me solve it. Thanks in advance
Upvotes: 1
Views: 2522
Reputation: 15303
You need to send the json string for creating a jsong object.
jsonObject = new JSONObject(json);
albums = jsonObject.getJSONArray("itemList");
Upvotes: 1
Reputation: 3415
If this task if not a educational/academic one I would recommend you to use a JSON Mapper. For example Jackson or GSON. "Manually" parsing JSON like this is just a waste of time and code and makes it much more error prone.
Doing the same things with a mapper would be two lines of code, literally.
Upvotes: 0