2014-11-21 146 views
0

我有一个JSON表,我试图解析一个URL。它始于节点{所以我解析为JSONObject。但我发现了以下错误:Android - 解析JSON失败

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个小时的错误。这是我向前推进App的唯一一步。感谢任何帮助。

编辑:

{ 
    "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" 
     } 
    ] 
} 
+0

发布JSON试图解析? – Geremy 2014-11-21 01:00:40

+0

你确定JSON格式正确吗?看到一个示例会很有帮助。 – 2014-11-21 01:00:41

+0

它附加在帖子上。 – rencsaridogan 2014-11-21 01:06:30

回答

1

你的代码读取响应在每行的端部插入的外来字符 'n':

  while ((line = reader.readLine()) != null) { 
      sb.append(line + "n"); 
      } 

尝试改变到

  while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
      } 
+0

我不确定JSON是否错误。我已经检查过它的语法10次了。 – rencsaridogan 2014-11-21 01:16:30

+0

谢谢你,以及Juanes。 – rencsaridogan 2014-11-21 01:24:25