0

I am trying to parse the JSON response get from the server and I am getting the error message: JSONArray cannot be converted to JSONObject.

Here Is the logcat:

08-28 18:56:08.083: W/System.err(1037): org.json.JSONException: Value [{"content":"<p class=\"bodytext\">erhalten Sie einen Überblick über die Aktivitäten der SKBF im Jahr 2011 im aktuellen Jahresbericht.<br \/><br \/><a href=\"de\/portraet\/auftrag\/#c113\" class=\"internal-link\" >SKBF Jahresbericht 2011<\/a><\/p>","pubDate":"01.06.12","category":"Allgemeine News","title":"SKBF Jahresbericht 2011","description":"Wenn Sie wissen wollen, was die SKBF macht","link":"http:\/\/www.skbf-csre.ch\/de\/news\/news-detail\/?tx_ttnews%5BbackPid%5D=2&tx_ttnews%5Btt_news%5D=80&cHash=1274043c236945bf6e592329e6742ebf"}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
08-28 18:56:08.083: W/System.err(1037): at org.json.JSON.typeMismatch(JSON.java:96)
08-28 18:56:08.083: W/System.err(1037): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
08-28 18:56:08.093: W/System.err(1037): at com.example.skbf_csre.SKBFActivity.onCreate(SKBFActivity.java:55)
08-28 18:56:08.093: W/System.err(1037): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
08-28 18:56:08.093: W/System.err(1037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
08-28 18:56:08.103: W/System.err(1037): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
08-28 18:56:08.103: W/System.err(1037): at android.app.ActivityThread.access1500ドル(ActivityThread.java:135)
08-28 18:56:08.103: W/System.err(1037): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
08-28 18:56:08.103: W/System.err(1037): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 18:56:08.113: W/System.err(1037): at android.os.Looper.loop(Looper.java:150)
08-28 18:56:08.113: W/System.err(1037): at android.app.ActivityThread.main(ActivityThread.java:4389)
08-28 18:56:08.113: W/System.err(1037): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 18:56:08.113: W/System.err(1037): at java.lang.reflect.Method.invoke(Method.java:507)
08-28 18:56:08.123: W/System.err(1037): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
08-28 18:56:08.123: W/System.err(1037): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-28 18:56:08.123: W/System.err(1037): at dalvik.system.NativeStart.main(Native Method)

What I amtrying to do is:

for (int i = 0; i < jsonArray.length(); i++) {
 JSONObject jsonObject = jsonArray.getJSONObject(i);
 System.out.println(jsonObject.getString("title"));
}

Thank you very much for your help.

James M
16.8k3 gold badges50 silver badges57 bronze badges
asked Aug 28, 2012 at 17:06
3
  • Please post a sample JSON document as test case. Thanks. Commented Aug 28, 2012 at 17:11
  • Thank you for the answer, the JSON can be found here: skbf-csre.ch/apps/iphone/rss/… Commented Aug 28, 2012 at 17:11
  • How jsonArray is being achieved? Error at org.json.JSONArray.getJSONObject can be because what stored in jsonArray is no longer an array. Try making it JsonObject first then only jsonArray. Commented Aug 28, 2012 at 17:32

1 Answer 1

6

It looks like you aren't digging quite deep enough to get to your data. What you have is an array containing multiple arrays, each of which contains an object. Try this:

for (int i = 0; i < jsonArray.length(); i++) {
 JSONArray innerJsonArray = jsonArray.getJSONArray(i);
 JSONObject jsonObject = innerJsonArray.getJSONObject(0);
 System.out.println(jsonObject.getString("title"));
}
answered Aug 28, 2012 at 17:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.