Android值。。。无法将java.lang.String类型的转换为JSONArray
我的项目使用WCF从数据库中获取记录并以JSON格式返回,如下所示:Android值。。。无法将java.lang.String类型的转换为JSONArray,android,json,wcf,arrays,Android,Json,Wcf,Arrays,我的项目使用WCF从数据库中获取记录并以JSON格式返回,如下所示: {"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\"}]"}
{"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\"}]"}
{"GetNotesResult":[{"ID":1,"Title":"Note1","Content":"HelloVuChienThang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note2","Content":"HelloNguyenThiNgoc","CreatedBy":"thangvc"}]}
我还有一个android应用程序来使用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解析器网站上,它解析得很好。请帮我解决这个问题 在json中,
GetNotesResult
的值包含在“
中,因此它被视为字符串而不是数组
要解析为数组,它应该是
{"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\"}]}
因此,解决方案是两件事之一:
”
。
或String notes = jobj.getString("GetNotesResult");
jarray = new JSONArray(notes);
\“
替换为”
,然后从[
之前和]
之后删除“
符号
像这样:
{"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\"}]"}
{"GetNotesResult":[{"ID":1,"Title":"Note1","Content":"HelloVuChienThang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note2","Content":"HelloNguyenThiNgoc","CreatedBy":"thangvc"}]}
这不是正确的JsonArrayOf当然我已经在清单文件中添加了INTERNET Permission。我甚至用另一个函数登录到我的android应用程序以连接服务器亲爱的Sameer,我使用LINQtoSQL DataContext和JavaScriptSerializer来序列化结果,这是我从浏览器中获得的原始JSON,你能告诉我更多关于这个问题吗JSON格式的m?
\“
也不会解析。看看我的答案。正是我想要的。谢谢你的答案。我不仅仅是粘贴我在浏览器中得到的JSON。我复制了我在代码中提到的JSONException中得到的值并粘贴到。