我遇到了字符串无法转换为JSONObject.Anyone的问题,可以帮助解决这个问题吗?谢谢,非常感谢你的帮助。
protected void onPostExecute(String result) {
if (result==null || result.length()==0){
// no result:
return;
}
//clear the list
moviesList.clear();
try {
//turn the result into a JSON object
JSONObject responseObject = new JSONObject("results");
// get the JSON array named "results"
JSONArray resultsArray = responseObject.getJSONArray(result);
// Iterate over the JSON array:
for (int i = 0; i < resultsArray.length(); i++) {
// the JSON object in position i
JSONObject messageObject = resultsArray.getJSONObject(i);
// get the primitive values in the object
String title = messageObject.getString("title");
String details = messageObject.getString("synopsis");
//put into the list:
Movie movie = new Movie(title, details, null,null);
moviesList.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
//refresh listView:
adapter.notifyDataSetChanged();
}
} 结果有价值
错误在以下行中:
JSONObject responseObject = new JSONObject("results");发布于 2013-11-13 07:23:11
String obj=JSONObject.quote(YourData);
JSONArray lArray=new JSONArray(obj);
// or simply Delete the prefix 'results' from your php Code
// $res2=array("results"=>$response);
// and you will retrive directelly your JsonArray like
JSONArray lArray=new JSONArray(YouData);发布于 2013-11-13 07:22:18
JSONObject格式是错误的。参见How to convert String to JSONObject in Java。只是"results"不是JSON。试着这样做:{"result":"blahblah"}
result中包含了双引号JSONObject responseObject = new JSONObject("results");
由于result已经是一个字符串,请尝试将该行替换为:
JSONObject responseObject = new JSONObject(results);
发布于 2013-11-13 08:24:53
看起来你把这两行弄混了,这样试试:
//turn the result into a JSON object
JSONObject responseObject = new JSONObject(result);
// get the JSON array named "results"
JSONArray resultsArray = responseObject.getJSONArray("results");这是假设你从某个地方得到的JSON响应包含一个结果,而这个结果又包含了一个‘JSONObject’。
从您的代码示例中的注释判断,情况就是这样,这只是一个由于相似命名而导致的简单混淆。
https://stackoverflow.com/questions/19942016
复制相似问题