Question

I am trying to pull some values out of a JSONObject, from this: http://proopt.co.za.winhost.wa.co.za/api/Course/INFO3003

My code is as follows:

        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();
        }

But when I run it, I get this error:

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)

I am aware that there is a problem with converting the jsonarray to an object (or vice versa) but I don't understand enough to fix it myself. Can anyone advise?

Was it helpful?

Solution

Your JSON

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

This is a JSON object not a JSON array. So instead of using:

JSONArray arr = new JSONArray(result);

you should use this:

JSONObject json = new JSONObject(result);

OTHER TIPS

What you have is a json object. Try the below

{ // 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();
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top