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)keys.next();
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.
JSONObject value = jObj.getJSONObject(key);
用。。。来代替
JSONObject value = values.getJSONObject(key);
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)keys.next();
JSONObject value = values.getJSONObject(key);
...
}
链接地址: http://www.djcxy.com/p/20120.html