-1

我正在尝试从 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 转换为对象(反之亦然)存在问题,但我对自己的理解还不够。任何人都可以建议吗?

4

2 回答 2

2

你的 JSON

{ 
    "CourseId": "INFO3003",   
    "CourseObjective": "To Be Awesome Sauce",
    "CourseDescription": "AWEsome shiznit",
    "Lecturer": "Mike",
    "Venue": "Senior Infos Lab"
}

这是一个 JSON 对象而不是 JSON 数组。所以不要使用:

JSONArray arr = new JSONArray(result);

你应该使用这个:

JSONObject json = new JSONObject(result);
于 2013-08-23T15:48:46.383 回答
2

你所拥有的是一个 json 对象。试试下面

{ // json object node
    "CourseId": "INFO3003",   
    "CourseObjective": "To Be Awesome Sauce",
    "CourseDescription": "AWEsome shiznit",
    "Lecturer": "Mike",
    "Venue": "Senior Infos Lab"
}
try {
JSONObject jb = new JSONObject("myjson");
String id = jb.getString("CourseId");
String CourseObjective = jb.getString("CourseObjective");
String CourseDescription = jb.getString("CourseDescription");
String Lecturer = jb.getString("Lecturer");
String Venue = jb.getString("Venue");
} catch (JSONException e) {
    e.printStackTrace();
} 
于 2013-08-23T15:48:59.140 回答