简体   繁体   English

无法从String解析为JSONObject

[英]Can't parse from String to JSONObject

Im using the following code to convert from a well-formed-String to a JSONObject in Java: 我使用以下代码从格式良好的String转换为Java中的JSONObject:

public void getStatus(){
    String status = "";
    try{
        String line;
        StringBuilder result = new StringBuilder("");
        HttpURLConnection conn;
        URL url;
        url = new URL("http://bromio.com.mx/explore/PointOfInterestService.svc/GetData");
        BufferedReader rd;
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        rd.close();
        Log.d("JSON: ", result.toString());
        JSONObject jsonObject = new JSONObject(result.toString());
    }catch (Exception e){
        e.printStackTrace();
    }
}

But it's throwing an exception, it seems that i cannot cast that specific string to JSONObject. 但它抛出异常,似乎我无法将该特定字符串强制转换为JSONObject。 The JSON can be acceses in this page: (It's too big to show!) http://bromio.com.mx/explore/PointOfInterestService.svc/GetData Any help solving this problem will help me a lot. JSON可以在这个页面中加入:(它太大了,无法显示!) http://bromio.com.mx/explore/PointOfInterestService.svc/GetData解决这个问题的任何帮助都会对我有所帮助。 thank you 谢谢

Let's take the first few characters of your json 我们来看看你的json的前几个字符

 "{ \"poi\":[\u000d\u000a  
 ^  ^    ^   ^^^^^^
 |  |    |      |-----------> not sure about all this
 |  |    -------------------> not acceptable in json
 |  ------------------------> not acceptable in json
 ---------------------------> not acceptable in json

What you get back from that website is not valid root json. 您从该网站获得的内容不是有效的根json。 Fix your source if you have control over it. 如果您可以控制它,请修复您的来源。 Or use the String utility methods to replace those characters. 或使用String实用程序方法替换这些字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM