我得到了这个JSON字符串:
[
{
"id": 135,
"date": "2013-08-30 19:00:29",
"timestamp": "2013-08-30 19:00:29",
"lat": "54.328274",
"long": "-2.747215",
"strap": "annual International Festival of Street Arts",
"link": "http://dev.website.co.uk//?p=135",
"title": "Title"
}
]我确信这是正确的JSON语法(在iOS应用程序中工作得很好),但是当解析到JSONObject时,它捕获了一个错误。Java:
public static JSONObject getJSONfromURL(String url){
//initialize
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
//try parse the string to a JSON object
try {
Log.d("log_tag", "jresult: " + result + "finish");
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}JSON中的某个地方是否存在错误?
发布于 2013-06-28 22:03:14
[表示json数组节点
{表示json对象节点
JSONArray jArray = new JSONArray(result);
return jArray; 同样,你可以有一个try块而不是多个try块。
发布于 2013-06-28 22:03:05
它是一个数组,所以您需要执行"new JSONArray()“而不是"new JSONObject()”。
发布于 2013-06-28 22:04:03
当json字符串以[开头时,它将被视为需要执行new JSONArray()的JSONArray
https://stackoverflow.com/questions/17366459
复制相似问题