我正在尝试从 JSONObject 中提取一些值:http: //proopt.co.za.winhost.wa.co.za/api/Course/INFO3003
我的代码如下:
String[] aJsonString = new String[10];
JSONObject jObject = null;
try {
JSONArray arr = new JSONArray(result);
for (int i = 0; i < arr.length(); i++) {
jObject = arr.getJSONObject(i);
}
aJsonString[0] = jObject.getString("CourseId");
aJsonString[1] = jObject.getString("CourseObjective");
aJsonString[2] = jObject.getString("CourseDescription");
aJsonString[3] = jObject.getString("Lecturer");
aJsonString[4] = jObject.getString("Venue");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但是当我运行它时,我得到了这个错误:
08-23 17:44:07.849: W/System.err(3150): org.json.JSONException: Value {"CourseDescription":"AWEsome shiznit","Lecturer":"Mike","Venue":"Senior Infos Lab","CourseObjective":"To Be Awesome Sauce","CourseId":"INFO3003"} of type org.json.JSONObject cannot be converted to JSONArray
08-23 17:44:07.859: W/System.err(3150): at org.json.JSON.typeMismatch(JSON.java:111)
08-23 17:44:07.859: W/System.err(3150): at org.json.JSONArray.<init>(JSONArray.java:91)
08-23 17:44:07.859: W/System.err(3150): at org.json.JSONArray.<init>(JSONArray.java:103)
08-23 17:44:07.859: W/System.err(3150): at com.example.wsis.CourseDetails$ServerCalling.onPostExecute(CourseDetails.java:116)
08-23 17:44:07.859: W/System.err(3150): at com.example.wsis.CourseDetails$ServerCalling.onPostExecute(CourseDetails.java:1)
08-23 17:44:07.869: W/System.err(3150): at android.os.AsyncTask.finish(AsyncTask.java:602)
08-23 17:44:07.869: W/System.err(3150): at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-23 17:44:07.869: W/System.err(3150): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-23 17:44:07.869: W/System.err(3150): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 17:44:07.869: W/System.err(3150): at android.os.Looper.loop(Looper.java:154)
08-23 17:44:07.879: W/System.err(3150): at android.app.ActivityThread.main(ActivityThread.java:4945)
08-23 17:44:07.879: W/System.err(3150): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:44:07.879: W/System.err(3150): at java.lang.reflect.Method.invoke(Method.java:511)
08-23 17:44:07.879: W/System.err(3150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-23 17:44:07.889: W/System.err(3150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-23 17:44:07.889: W/System.err(3150): at dalvik.system.NativeStart.main(Native Method)
我知道将 jsonarray 转换为对象(反之亦然)存在问题,但我对自己的理解还不够。任何人都可以建议吗?