JSON type mismatch or org.json.jsonecxeption

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

The link is http://iipacademy.in/askpoll/ten_feed.php

exception is in onPostExecute() method (4th line) :

Log.i("result", result); try {     if (result != null) {         JSONArray jsonArray = new JSONArray(result); // erreor         for (int i = 0; i 

LOGCAT:

12-18 03:20:45.447: W/System.err(2790): org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONArray 12-18 03:20:45.447: W/System.err(2790):     at org.json.JSON.typeMismatch(JSON.java:111) 12-18 03:20:45.447: W/System.err(2790):     at org.json.JSONArray.(JSONArray.java:91) 12-18 03:20:45.447: W/System.err(2790):     at org.json.JSONArray.(JSONArray.java:103) 12-18 03:20:45.447: W/System.err(2790):     at com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:188) 12-18 03:20:45.447: W/System.err(2790):     at com.example.askpollie.LatestPollParticipated$FetchingEventsDetails.onPostExecute(LatestPollParticipated.java:1) 12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask.finish(AsyncTask.java:631) 12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask.access$600(AsyncTask.java:177) 12-18 03:20:45.447: W/System.err(2790):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 12-18 03:20:45.447: W/System.err(2790):     at android.os.Handler.dispatchMessage(Handler.java:99) 12-18 03:20:45.447: W/System.err(2790):     at android.os.Looper.loop(Looper.java:137) 12-18 03:20:45.447: W/System.err(2790):     at android.app.ActivityThread.main(ActivityThread.java:5103) 12-18 03:20:45.447: W/System.err(2790):     at java.lang.reflect.Method.invokeNative(Native Method) 12-18 03:20:45.447: W/System.err(2790):     at java.lang.reflect.Method.invoke(Method.java:525) 12-18 03:20:45.447: W/System.err(2790):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 12-18 03:20:45.447: W/System.err(2790):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-18 03:20:45.447: W/System.err(2790):     at dalvik.system.NativeStart.main(Native Method) 12-18 03:20:45.447: D/dalvikvm(2790): GC_FOR_ALLOC freed 5131K, 55% free 4437K/9672K, paused 2ms, total 2ms 

Message is an array so what should be its code or how to can it be solved ?

Thanks in advance . . .

回答1:

org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONArray 

Looks like response is a string not a json array

{  // json object node      "response": { // json object response         "result": 1,         "Message": [ // json array Message             {        // json object node                  "pollid": "98",                  "category": "Entertainment",                  "question": "what",  //  string                  "option1": "981.mov", 

The result is a json object not json array

JSONArray jsonArray = new JSONArray(result); 

Should be

JSONObject jObj = new JSONObject(result); JSONObject response = jObj.getJSONObject("response"); //JSONObject jb = new JSONObject(response); JSONArray jr = response.getJSONArray("Message"); for(int i=0;i


回答2:

Try this:

Log.i("result", result); try {     if (result != null) {         JSONObject jObject = new JSONObject(result);         JSONArray jsonArray = jObject.getJSONObject("response").getJSONArray("Message");         for (int i = 0; i 

As mario and nfear said, you are trying to cast a JSONObject into a JSONArray.



回答3:

String content JSONObject as root element instead of JSONArray.to get Message JSONArray from String you should first get response JSONObject then get Message JSONArray as:

 JSONObject jsonobj = new JSONObject(result);  // get response JSONObject   JSONObject jsonobj_response = jsonobj.getJSONObject("response");  // get Message JSONArray from jsonobj_response   JSONArray jsonArray = jsonobj_response.getJSONArray("Message");   // your code here..... 


回答4:

Try this,

try {     if (result != null) {         JSONArray jsonArray = new JSONObject(result).getJSONObject("response").getJSONArray("Message");         for (int i = 0; i 


回答5:

The JSON you have posted is having

// Key "response" , Value type JsonObject   JSONObject jsonObject=jsonObjectOfRsponse.getJsonObject(key);  //Key "Message" , Value type Json Array   JSONArray jsonArray=jsonObject.getJsonArray(key); 

After getting the jsonArray, use this in a loop to parse the Json according to the type of value inside it.

So check if you are parsing the json according to the type of value it is holding.



回答6:

Result is not an array, Message is.



回答7:

Value response of type java.lang.String cannot be converted to JSONArray 

You are trying to convert a single object into an array.



回答8:

better to use GSON library instead of parsing it manually. GSON you can find it on goggling. and sample examples are given with that.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!