Having trouble getting value from JSOnObject

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?

Answers


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

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

Need Your Help

Is it possible to change the subject of the original mail with php before reaching the inbox?

php email cakephp

i am currently reading the mails from mailserver and i would like to change the subject of the all the mail in the mailserver with appending some character to it and send it to user inbox.

Colour-coding text runs in a FlowDocument

wpf xaml binding converter dependency-properties

I’m trying to produce a FlowDocument, and want certain text runs to be highlighted when they contain a particular string. I’m using the BindableRun class to ensure I can update the Run text values

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.