我正在编写一个简单的android应用,该应用使用API来获取一些数据,但是出现以下错误:
System.err: org.json.JSONException: No value for genre_ids
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:584)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.adeel.android.movieinfoapp.Networking.onPostExecute(Networking.java:72)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.adeel.android.movieinfoapp.Networking.onPostExecute(Networking.java:21)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:632)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.os.Looper.loop(Looper.java:145)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5832)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
07-19 18:12:08.164 24204-24204/com.adeel.android.movieinfoapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
下面是我的代码:
@Override
protected void onPostExecute(String s) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonStrings = jsonObject.getJSONArray("genre_ids");
String strings[] = new String[jsonStrings.length()];
for(int i=0;i<strings.length;i++) {
strings[i] = jsonStrings.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
super.onPostExecute(s);
}
}
我尝试编写的应用程序使用此JSON。
最佳答案
您没有正确解析嵌套的JsonArray
。
这里genre_ids
数组在results
数组下
使用以下代码:
try {
JSONObject jsonObject = new JSONObject(s); //string pass to your onPostExecute()
JSONArray result = jsonObject.getJSONArray("results");
for(int j=0;i<result.length;j++) {
JSONObject inner = result.getJSONObject(j);
JSONArray jsonStrings = inner.getJSONArray("genre_ids");
String strings[] = new String[jsonStrings.length()];
for(int i=0;i<strings.length;i++) {
strings[i] = jsonStrings.getString(i);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
关于java - JSON异常(exception):无值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45199963/