smurfMT smurfMT - 1 year ago 62
JSON Question

How to resolve, JSONException: No value for "1"

been looking around on the site for a while, and i'm not getting anywhere.

I am creating an android app which is getting a list of values from a mySQL DB via PHP, and returning the result encoded in a JSON object:

{"error":false,"values":{"1":{"name":"This is a string"}, "2":{...}}}

I am retrieving the JSON object in java like so:

JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");

if (!error) {
JSONObject values = jObj.getJSONObject("values");
Iterator<?> keys = values.keys();

while(keys.hasNext()) {
String key = (String);
JSONObject value = jObj.getJSONObject(key);

Its at the point before the ellipses where I get the exception:

org.json.JSONException: No value for "1"

Am I doing something completely stupid, or what, everything looks ok to me. I have tried multiple different things but keep getting the same issue.
What i would ultimately like to do is get the name obj and add it to a List of strings. But I can't seem to get beyond this point.

Any help or push in the right direction would be grateful.

Answer Source
JSONObject value = jObj.getJSONObject(key);

Replace with

JSONObject value = values.getJSONObject(key);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download