2013-05-16 88 views
0

我可以從url.But獲得字符串響應當我嘗試json解析該響應時,它顯示了一些像下面的錯誤。任何人都可以提供任何想法來清除它。這是我的Api.java文件。執行JSON解析時發生錯誤。

錯誤:

05-16 04:16:19.314: W/System.err(1582): org.json.JSONException: Value [] at needAttachment of type org.json.JSONArray cannot be converted to JSONObject 
05-16 04:16:19.314: W/System.err(1582):  at org.json.JSON.typeMismatch(JSON.java:100) 
05-16 04:16:19.323: W/System.err(1582):  at org.json.JSONObject.getJSONObject(JSONObject.java:573) 
05-16 04:16:19.323: W/System.err(1582):  at com.example.twitterjson.TwitterApi.getconnect(TwitterApi.java:59) 
05-16 04:16:19.323: W/System.err(1582):  at com.example.twitterjson.TwitterApi.doInBackground(TwitterApi.java:31) 

JsonApi.java

package com.example.twitterjson; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.os.AsyncTask; 

public class TwitterApi extends AsyncTask<Void, Integer, Void> implements 
     TwitterArraylist { 
    MainActivity activity; 
    JSONArray jsonarray; 

    public TwitterApi(MainActivity activity) { 
     this.activity = activity; 
     arraylist.clear(); 

    } 

    protected void onPreExecute() { 

     super.onPreExecute(); 
    } 

    protected void onPostExecute(Void result) { 
     activity.setup(); 
     super.onPostExecute(result); 
    } 

    protected Void doInBackground(Void... param) { 
     getconnect(); 
     return null; 

    } 

    public void getconnect() { 
     String url = "Some url"; 
     InternetManager manager = new InternetManager(url); 
     String contentresponse = manager.Urlreq(); 
     if (contentresponse != null) { 
      System.out.println("content_jsonresponse= " + contentresponse); 
      try { 

       JSONObject mainobject = new JSONObject(contentresponse); 
       JSONArray needarray = mainobject.getJSONArray("needs"); 
       for (int i = 0; i < needarray.length(); i++) { 
        JSONObject needarrayobject = needarray.getJSONObject(i); 
        JSONObject needobject = needarrayobject 
          .getJSONObject("need"); 
        TwitterModel t = new TwitterModel(); 
        t.setId(needobject.getString("id")); 
        t.setText(needobject.getString("description")); 
        t.setTime(needobject.getString("created")); 
        JSONObject needattach = needarrayobject 
          .getJSONObject("needAttachment"); 
        t.setName(needattach.getString("title")); 
        arraylist.add(t); 

       } 

       System.out.println("CONTENT_MODEL.size()" + arraylist.size()); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

我的JSON:

{ 
    "needs": [ 
    { 
     "need": { 
     "id": "197", 
     "title": "A new roof1", 
     "description": "Im hoping this shit works.", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-04-12 06:04:04" 
     }, 
     "needAttachment": [ 

     ] 
    }, 
    { 
     "need": { 
     "id": "196", 
     "title": "Test10", 
     "description": "This is the test 10 one", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-04-03 03:47:24" 
     }, 
     "needAttachment": [ 

     ] 
    }, 
    { 
     "need": { 
     "id": "194", 
     "title": "Draft test", 
     "description": "Test test", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-29 06:10:36" 
     }, 
     "needAttachment": { 
     "id": "118", 
     "title": null, 
     "file": "\/files\/uploads\/need\/f6ad4d9a473458da7a9aabfec4fdef1b" 
     } 
    }, 
    { 
     "need": { 
     "id": "192", 
     "title": "kitchen sink", 
     "description": "My kitchen sink is clogged", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-28 11:15:51" 
     }, 
     "needAttachment": { 
     "id": "117", 
     "title": null, 
     "file": "\/files\/uploads\/need\/4a49d9eda8aab623e4dcb6fafd124115" 
     } 
    }, 
    { 
     "need": { 
     "id": "191", 
     "title": "test5", 
     "description": "This is test5", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-28 07:23:57" 
     }, 
     "needAttachment": { 
     "id": "120", 
     "title": null, 
     "file": "\/files\/uploads\/need\/31eba0e0570c6d16df7df8706c1dbcb3" 
     } 
    }, 
    { 
     "need": { 
     "id": "190", 
     "title": "tree removal", 
     "description": "tree removal", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-26 18:25:52" 
     }, 
     "needAttachment": { 
     "id": "110", 
     "title": null, 
     "file": "\/files\/uploads\/need\/890724880928cda88d3d665aaad2239d" 
     } 
    }, 
    { 
     "need": { 
     "id": "189", 
     "title": "Roof 3", 
     "description": "Roof", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-25 15:08:33" 
     }, 
     "needAttachment": { 
     "id": "99", 
     "title": null, 
     "file": "\/files\/uploads\/need\/3fff1990c035edba6fb2a728f247ef7b" 
     } 
    }, 
    { 
     "need": { 
     "id": "188", 
     "title": "test3", 
     "description": "Test3 done here", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-25 04:04:45" 
     }, 
     "needAttachment": { 
     "id": "102", 
     "title": null, 
     "file": "\/files\/uploads\/need\/f3e3a58076441b73000c098e5c5884b7" 
     } 
    }, 
    { 
     "need": { 
     "id": "187", 
     "title": "MyBook1", 
     "description": "Test for MyBook1", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-24 23:13:56" 
     }, 
     "needAttachment": { 
     "id": "104", 
     "title": null, 
     "file": "\/files\/uploads\/need\/61d3daf4f8bc67706d86577303bacd75" 
     } 
    }, 
    { 
     "need": { 
     "id": "186", 
     "title": "spring cleaning", 
     "description": " Time for spring clean. testing the edit feature. ", 
     "location_id": "1", 
     "status": "1", 
     "created": "2013-03-24 18:47:09" 
     }, 
     "needAttachment": { 
     "id": "107", 
     "title": null, 
     "file": "\/files\/uploads\/need\/0914f9b5fa843c0ee8bebea109c9444d" 
     } 
    } 
    ] 
} 
+0

在日誌'needAttachment'中是一個'JSONArray'而不是'JSONObject',因此您需要將它轉換爲'JSONArray'而不是'JSONObject' –

+0

發佈您的JSON。 – SKK

回答

0

由於錯誤描述,您試圖解析的needAttachments作爲值a JSONObject

JSONObject needattach = needarrayobject.getJSONObject("needAttachment"); 

雖然它是JSONArray。所以你真正需要解析它是這樣:

JSONArray needattach = needarrayobject.getJSONArray("needAttachment"); 
0

在你的Json needAttachment是一個對象,但是當它是空的它是一個空數組。 因此,如果你有控制權,請將空數組更改爲Json數據中的空對象{},或者捕獲異常並將其解析爲數組,如其他驅動程序中所述。

相關問題