Android-解析JSON时失败
我试图解析的URL上有一个JSON表。它以节点{开始,因此我将解析为JSONObject。但我得到以下错误:Android-解析JSON时失败,android,json,parsing,http,url,Android,Json,Parsing,Http,Url,我试图解析的URL上有一个JSON表。它以节点{开始,因此我将解析为JSONObject。但我得到以下错误: 11-21 02:33:38.660: E/JSON Parser(20307): Error parsing data org.json.JSONException: Expected ':' after n at character 7 of {n "videos": [n {n "title": "Big Buck Bunny",n
11-21 02:33:38.660: E/JSON Parser(20307): Error parsing data org.json.JSONException: Expected ':' after n at character 7 of {n "videos": [n {n "title": "Big Buck Bunny",n "thumbnail": "http://camendesign.com/code/video_for_everybody/poster.jpg",n "video_url": "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"n },n {n "title": "Perfect Lamborghini Impression",n "thumbnail": "http://www.mp4point.com/images/thumb/perfect-lamborghini-impression.jpg",n "video_url": "http://www.mp4point.com/downloads/7b0de690da55.mp4"n },n {n "title": "Flute Beatboxing",n "thumbnail": "http://www.mp4point.com/images/thumb/flute-beatboxing.jpg",n "video_url": "http://www.mp4point.com/downloads/bd8bda782093.mp4"n }n ]n}n
我正在使用下面的代码进行解析:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser(){
}
public JSONObject getJSONfromURL(String url){
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
有什么想法可能会导致这里的问题吗?这真的很令人沮丧,我正在试图找出这里的错误大约3个小时了。这是我唯一的一步前进的应用程序。感谢任何帮助
编辑:
{
"videos": [
{
"title": "Big Buck Bunny",
"thumbnail": "http://camendesign.com/code/video_for_everybody/poster.jpg",
"video_url": "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
},
{
"title": "Perfect Lamborghini Impression",
"thumbnail": "http://www.mp4point.com/images/thumb/perfect-lamborghini-impression.jpg",
"video_url": "http://www.mp4point.com/downloads/7b0de690da55.mp4"
},
{
"title": "Flute Beatboxing",
"thumbnail": "http://www.mp4point.com/images/thumb/flute-beatboxing.jpg",
"video_url": "http://www.mp4point.com/downloads/bd8bda782093.mp4"
}
]
}
读取响应的代码正在每行末尾插入无关的“n”字符:
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
试着把它改成
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
发布它试图解析的JSON?你确定JSON格式正确吗?看到一个示例会很有帮助。它被附加在帖子上。这一行做什么:sb.appendline+n;?它似乎在向JSON数据中添加一个n。这无助于你解析它。如果我读了,我将转到下一行,并在这一点上传递n。我已经尝试了pl是的,但它甚至不再读取数据。问题一定是别的。我不确定JSON是否错误。我已经检查了它的语法10次。谢谢你,胡安。