2013-04-11 44 views
4

有一個WCF從數據庫中獲取記錄,並以JSON格式返回像這樣我的項目:Android的價值... java.lang.String類型不能轉換成JSONArray

{"GetNotesResult":"[{\"ID\":1,\"Title\":\"Note 1\",\"Content\":\"Hello Vu Chien Thang\",\"CreatedBy\":\"thangvc\"},{\"ID\":2,\"Title\":\"Note 2\",\"Content\":\"Hello Nguyen Thi Ngoc\",\"CreatedBy\":\"thangvc\"}]"} 

我也有一個機器人應用程序消耗的JSON,這是我的代碼:

private JSONArray getNotes(String UserName, String Password) { 
     JSONArray jarray = null; 
     JSONObject jobj = null; 
     try{ 
      StringBuilder builder = new StringBuilder(URL); 
      builder.append("UserName=" + loggedInUser.getUserName()); 
      builder.append("&"); 
      builder.append("Password=" + loggedInUser.getPassword()); 

      HttpClient client = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(builder.toString()); 
      HttpResponse response = client.execute(httpGet); 

      int status = response.getStatusLine().getStatusCode(); 

      if(status==200) 
      { 
       HttpEntity entity = response.getEntity(); 
       String data = EntityUtils.toString(entity,"utf-8"); 
       jobj = new JSONObject(data); 
       jarray = jobj.getJSONArray("GetNotesResult"); 
      } 
      else 
      { 
       Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show(); 
      } 
     } 
     catch(ClientProtocolException e) 
     { 
      Log.d("ClientProtocol",e.getMessage()); 
     } 
     catch(IOException e) 
     { 
      Log.d("IOException", e.getMessage()); 
     } 
     catch(JSONException e) 
     { 
      Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
     catch(Exception e) 
     { 
      Log.d("Unhandle Error", e.getMessage()); 
     } 
     return jarray; 
    } 

我在jarray = jobj.getJSONArray("GetNotesResult");設置斷點,並從JSONException此消息:

Value [{"ID":1,"Title":"Note 1","Content":"Hello Vu Chien Thang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note 2","Content":"Hello Nguyen Thi Ngoc","CreatedBy":"thangvc"}] at GetNotesResult of type java.lang.String cannot be converted to JSONArray 

我試圖複製JSON字符串並粘貼到在線JSON解析器網站http://jsonviewer.stack.hu/並且解析得很好。請幫我解決這個問題!

+0

這JSON你長了? – kyogs 2013-04-11 09:39:07

+0

這是不正確的JsonArray – Sameer 2013-04-11 09:41:12

+0

當然,我已經添加INTERNET permssion清單文件。我甚至登錄到我的android應用程序與另一個函數連接到服務器 – user2269607 2013-04-11 09:43:28

回答

16

在你的JSON GetNotesResult值內""所以inclused它被認爲是一個字符串,而不是一個數組..

被解析爲一個數組應該..

{"GetNotesResult":[{\"ID\":1,\"Title\":\"Note 1\",\"Content\":\"Hello Vu Chien Thang\",\"CreatedBy\":\"thangvc\"},{\"ID\":2,\"Title\":\"Note 2\",\"Content\":\"Hello Nguyen Thi Ngoc\",\"CreatedBy\":\"thangvc\"}]} 

因此,解決辦法是兩件事之一:

  1. 如果您可以修改服務器響應,請將json數組周圍的""刪除。 或
  2. 解析它首先是作爲字符串,然後從字符串創建一個JSON數組..

    String notes = jobj.getString("GetNotesResult"); 
    jarray = new JSONArray(notes); 
    
+4

+1準確。你抓住了我的回答;) – 2013-04-11 09:40:59

+0

不錯的一個catch .. +1 – Sameer 2013-04-11 09:41:42

+0

''''也不會解析。看我的回答。 – Budius 2013-04-11 09:43:04

0

我試圖將其粘貼到http://jsonviewer.stack.hu/並沒有奏效。

使它工作我不得不更換所有\""然後[之前,從取出"符號和之後的]

這樣的:

{"GetNotesResult":[{"ID":1,"Title":"Note1","Content":"HelloVuChienThang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note2","Content":"HelloNguyenThiNgoc","CreatedBy":"thangvc"}]} 
+0

我不只是粘貼我在瀏覽器中得到的JSON。我複製了我在JSONException中得到的值,我在上面的代碼中提到了並粘貼到http://jsonviewer.stack.hu/。 – user2269607 2013-04-11 09:54:31

相關問題