2013-12-18 35 views
5

鏈路是 http://iipacademy.in/askpoll/ten_feed.phpJSON類型不匹配或org.json.jsonecxeption

的例外是在onPostExecute()方法(第4行):

Log.i("result", result); 
try { 
    if (result != null) { 
     JSONArray jsonArray = new JSONArray(result); // erreor 
     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject objJson = jsonArray.getJSONObject(i); 

      TopTenGetterSetter obj = new TopTenGetterSetter(); 


      obj.setQ(objJson.getString("question")); 
      obj.setA(objJson.getString("option1")); 
      obj.setB(objJson.getString("option2")); 
      obj.setC(objJson.getString("option3")); 
      obj.setD(objJson.getString("option4")); 

      polls.add(obj); 
         } 

    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), "error", 
      Toast.LENGTH_SHORT).show(); 
} 

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.<init>(JSONArray.java:91) 
12-18 03:20:45.447: W/System.err(2790):  at org.json.JSONArray.<init>(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 

消息是一個數組,所以它的代碼應該是什麼或者如何解決?

謝謝 in advance。 。 。

+0

給定的webservice返回'JSONObject'作爲根元素而不是'JSONArray' –

+0

你可以使用'new org.json.simple.parser.JSONParser()。parse(「jsonString」);'來獲得JsonArray。 – Runcorn

+0

發佈您的json數據 –

回答

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

看起來像響應是一個字符串不是一個JSON數組

{ // 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", 

結果是JSON對象不JSON數組

JSONArray jsonArray = new JSONArray(result); 

應該是

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<jr.length();i++) 
{ 
JSONObject jb1 = jr.getJSONObject(i); 
String question = jb1.getString("question"); 
Log.i(".......",question); 
} 
0

結果不是數組,消息是。

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

您試圖將單個對象轉換爲數組。

1

試試這個:

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 < jsonArray.length(); i++) { 
      JSONObject objJson = jsonArray.getJSONObject(i); 

      TopTenGetterSetter obj = new TopTenGetterSetter(); 
      /* 
      * [{"job_id":"1","job_staff_id":"","job_name":"Account", 
      * "job_detail":"test\r\ntesds","job_start_date": 
      * "2013-11-08" 
      * ,"job_end_date":"2013-11-10","job_amount":"500" 
      * ,"job_progress":"","job_complete_status":"0"}] 
      */ 

      obj.setQ(objJson.getString("question")); 
      obj.setA(objJson.getString("option1")); 
      obj.setB(objJson.getString("option2")); 
      obj.setC(objJson.getString("option3")); 
      obj.setD(objJson.getString("option4")); 

      polls.add(obj); 
      // am = Customer.info.get(pos).getJamount(); 

      // Toast.makeText(getApplicationContext(), am + result, 
      // Toast.LENGTH_LONG).show(); 
     } 

    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), "error", 
      Toast.LENGTH_SHORT).show(); 
} 

馬里奧和nfear說,你想投一個JSONObject成JSONArray。

1

字符串內容的JSONObject的根元素,而不是從JSONArray.to字符串得到Message JSONArray你應該先response的JSONObject然後得到Message JSONArray爲:

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..... 
1

試試這個,

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

      TopTenGetterSetter obj = new TopTenGetterSetter(); 
      /* 
      * [{"job_id":"1","job_staff_id":"","job_name":"Account", 
      * "job_detail":"test\r\ntesds","job_start_date": 
      * "2013-11-08" 
      * ,"job_end_date":"2013-11-10","job_amount":"500" 
      * ,"job_progress":"","job_complete_status":"0"}] 
      */ 

      obj.setQ(objJson.getString("question")); 
      obj.setA(objJson.getString("option1")); 
      obj.setB(objJson.getString("option2")); 
      obj.setC(objJson.getString("option3")); 
      obj.setD(objJson.getString("option4")); 

      polls.add(obj); 
      // am = Customer.info.get(pos).getJamount(); 

      // Toast.makeText(getApplicationContext(), am + result, 
      // Toast.LENGTH_LONG).show(); 
     } 

    } 
} 
0

最好使用GSON庫,而不是手動解析它的。 GSON你可以在護目鏡上找到它。並給出樣例。

1

已發佈的JSON是有

// Key "response" , Value type JsonObject 

JSONObject jsonObject=jsonObjectOfRsponse.getJsonObject(key); 

//Key "Message" , Value type Json Array 

JSONArray jsonArray=jsonObject.getJsonArray(key); 

得到jsonArray後,用這一個循環按照裏面值的類型解析JSON。

因此,檢查你是否根據它所持有的值的類型來解析json。