2013-04-11 82 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