Android - 解析JSON时失败

时间:2014-11-21 00:58:59

标签: android json parsing http url

我在URL上有一个JSON表,我试图解析它。它以node {开头,因此我将解析为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            "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"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您读取响应的代码正在插入无关的' n'每行末尾的字符:

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

尝试将其更改为

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