在java for Android中读取JSON字符串时,如果有值要查找,则会显示错误。 json字符串来自(http://ddragon.leagueoflegends.com/cdn/7.24.1/data/en_US/champion.json)我需要帮助找出我在读取JSON字符串时出错的地方。
我得到的错误如下。
W/System.err: org.json.JSONException: No value for Aatrox
W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
W/System.err: at com.icyrelic.lolsummoner.api.LeagueAPI$3.onSuccess(LeagueAPI.java:146)
W/System.err: at com.icyrelic.lolsummoner.api.LeagueAPI$1.onResponse(LeagueAPI.java:51)
W/System.err: at com.icyrelic.lolsummoner.api.LeagueAPI$1.onResponse(LeagueAPI.java:41)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:78)
W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err: at android.os.Looper.loop(Looper.java:154)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6776)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
来自LeagueAPI.java的代码
public static void createRequest(String url, final VolleyCallback callback) {
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Object json = new JSONTokener(response).nextValue();
if(json instanceof JSONObject) {
callback.onSuccess(new JSONObject(response));
} else if(json instanceof JSONArray) {
callback.onSuccess(new JSONArray(response));
} else {
throw new Exception("Unknown JSON");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(stringRequest);
}
private void setupChampionData() {
createRequest("http://ddragon.leagueoflegends.com/cdn/7.24.1/data/en_US/champion.json", new VolleyCallback() {
@Override
public void onSuccess(Object response) {
JSONObject json = ((JSONObject) response);
try {
JSONObject champions = json.getJSONObject("data");
Iterator<String> temp = champions.keys();
System.out.println(json.toString());
while (temp.hasNext()) {
String key = temp.next();
JSONObject value = ((JSONObject) json.get(key));
championData.put(value.getInt("key"), value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
答案 0 :(得分:1)
您正在读取错误对象的值。试试这个
JSONObject value = ((JSONObject) champions.get(key));
而不是
JSONObject value = ((JSONObject) json.get(key));
答案 1 :(得分:0)
"type":"champion",
"format":"standAloneComplex",
"version":"7.24.1",
"data":{
"Aatrox":{ },
"Ahri":{ },
}..
如果你看到json响应“Aatrox”是数据对象内的JSONObject。修改对象阅读器。你可能会得到它。
答案 2 :(得分:0)
JSONObject jsonObjectRoot = new JSONObject("Your_Json");
JSONObject jsonObjectData = jsonObjectRoot.getJSONObject("data");
JSONObject jsonObjectAtrox = jsonObjectData.getJSONObject("Aatrox");
JSONObject jsonObjectAtroxInfo = jsonObjectAtrox.getJSONObject("info");
等等......