I am trying to pull some values out of a JSONObject, from this: http://proopt.co.za.winhost.wa.co.za/api/Course/INFO3003
我试图从JSONObject中提取一些值,来自: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?
我知道将jsonarray转换为对象存在问题(反之亦然),但我不了解自己修复它。任何人都可以建议吗?
2 个解决方案
#1
2
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:
这是一个JSON对象而不是JSON数组。所以不要使用:
JSONArray arr = new JSONArray(result);
you should use this:
你应该用这个:
JSONObject json = new JSONObject(result);
#2
2
What you have is a json object. Try the below
你拥有的是一个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();
}
#1
2
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:
这是一个JSON对象而不是JSON数组。所以不要使用:
JSONArray arr = new JSONArray(result);
you should use this:
你应该用这个:
JSONObject json = new JSONObject(result);
#2
2
What you have is a json object. Try the below
你拥有的是一个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();
}