【问题标题】:JSON Parsing Exception in AndroidAndroid 中的 JSON 解析异常
【发布时间】:2012-06-13 17:49:27
【问题描述】:

我有以下问题: 我在尝试在 Android 中解析的服务器上有一个 JSON 文件。但我收到以下错误消息:

06-13 19:24:39.025:E/JSON Parser(17169):解析数据时出错 org.json.JSONException: java.lang.String 类型的值 不能 转换为 JSONObject

这是我的 JSON 文件:

    {
"settings":[
  {
     "rss":"true",
     "rss_feed":"http://test.com/rss.rss"
  }
],
 "map_locations":[
  {
     "title":"Büro Toronto",
     "address":"123 Younge Street Toronto"
  },
  {
     "title":"Büro New York",
     "address":"Time Square New York"
  }
]
}

这是我的代码:

        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(SETTINGS_URL);

        try {
            JSONObject c = json.getJSONArray("settings").getJSONObject(0);

            rss = c.getBoolean("rss");

            JSONArray jMap = json.getJSONArray("map_locations");
            for (int i = 0; i < jMap.length(); i++) {
                JSONObject c2 = jMap.getJSONObject(i);

                String map_title = c2.getString("title");
                String map_address = c2.getString("address");

                mapListTitle.add(map_title);
                mapListAddress.add(map_address);
            }

            URL_TO_RSSFEED = c.getString("rss_feed");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

提前感谢您的帮助!

奇怪的是,我没有改变任何东西(据我所知)并且它之前确实有效。 如果您需要更多信息,请告诉我!

【问题讨论】:

  • 你到底是什么意思?像 UTF-8 问题之类的?我也试过用“ue”而不是“ü”,但这并没有改变任何东西......
  • @user754730 : 您可以从这里检查 api 返回的 json 是否有效jsonviewer.stack.hu
  • JSON 在网站上看起来确实不错...

标签: android json parsing jsonexception


【解决方案1】:

哇,好的,我找到了答案……不得不改变

BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);

to BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);

奇怪的是,它在几个小时前使用相同的设置确实可以工作。

非常感谢您的帮助!

【讨论】:

    【解决方案2】:

    我运行了这段代码,它运行良好。

    String jsonStr = "{\"settings\":[{\"rss\":\"true\",\"rss_feed\":\"http://test.com/rss.rss\"}],\"map_locations\":[{\"title\":\"Büro Toronto\",\"address\":\"123 Younge Street Toronto\""
                  +"},{\"title\":\"Büro New York\",\"address\":\"Time Square New York\"}]}";
        try {
        JSONObject json = new JSONObject(jsonStr);
    
    
            JSONObject c = json.getJSONArray("settings").getJSONObject(0);
    
            boolean rss = c.getBoolean("rss");
    
            JSONArray jMap = json.getJSONArray("map_locations");
            for (int i = 0; i < jMap.length(); i++) {
                JSONObject c2 = jMap.getJSONObject(i);
    
                String map_title = c2.getString("title");
                String map_address = c2.getString("address");
    
               /* mapListTitle.add(map_title);
                mapListAddress.add(map_address);*/
            }
    
           String URL_TO_RSSFEED = c.getString("rss_feed");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();  
    }
    

    所以我只能得出结论,您正在创建的第一个 jsonObject 可能存在一些错误,我正在谈论这一行:

    JSONObject json = jParser.getJSONFromUrl(SETTINGS_URL);

    【讨论】:

    • 我想你可能正在使用这篇文章中的readJsonFromUrl(String url) stackoverflow.com/questions/4308554/… ,@RolandIllig 给出的答案。如果是,那么您在JSONObject json = new JSONObject(jsonText); 这一行遇到了错误。
    • 感谢这让我了解了我的 JSONParser 类。您可以在下面找到我的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2012-10-19
    相关资源
    最近更新 更多