2014-11-21 94 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