【问题标题】:JSON parsing problemJSON解析问题
【发布时间】:2011-08-31 09:19:52
【问题描述】:

我正在尝试在我的 android 应用程序中进行 Json 解析,链接是 https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776

当我将它放入 Json 对象时,它会给我带来错误 错误是: 08-31 14:40:52.281: WARN/System.err(416): org.json.JSONException: java.lang.String 类型的值不能转换为 JSONObject

public static String getmyproductquestiondetails(String userid,
        String campaignid) {// https://www.buzzador.com/apps/present_software/webservice/index.php?op=EducationResult&userid=1&questionid=1,2,3&answergivenbyuser=1,1,0
    String data = null;
    try {

        URL url = new URL(
                "http://dignizant.com/buzz/webservice/index.php?op=getProductQuestion&userid="
                        + userid + "&campaign_id=" + campaignid);
        if (url.getProtocol().toLowerCase().equals("https")) {
            trustAllHosts();
            HttpsURLConnection https = (HttpsURLConnection) url
                    .openConnection();
            https.setHostnameVerifier(DO_NOT_VERIFY);
            http = https;
        } else {
            http = (HttpURLConnection) url.openConnection();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Utils utils = new Utils();

    try {
        data = utils.convertStreamToString(http.getInputStream());
        System.out.println("getproduct details response :: " + data);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        data = e.toString();

    }
    return data;
}







try {


        JSONObject jo = new JSONObject(response);

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

【问题讨论】:

  • 请发布您正在使用的代码。
  • 如果您可以发布代码,您的解析器就会崩溃。这将非常有帮助。
  • 发布的示例代码不使用任何与 JSON 相关的内容。可能是您发布了错误的部分?
  • @Robert 现在我发布了 json 代码

标签: java android json


【解决方案1】:
  char[] utf8 = null;
            StringBuilder properString = new StringBuilder("");

            utf8 = Response.toCharArray();

            for (int i = 0; i < utf8.length; i++) {
                if ((int) utf8[i] < 65000) {
                    properString.append(utf8[i]);
                }
            }
  System.out.println("Response of Login::"
                        + properString.toString());

【讨论】:

  • 经过大量尝试后,我得到了答案。我们必须这样对待响应。
  • 嗯,这很好,可以用于其他开发人员,所以我正在做 +1。
  • 您,先生,是救生员。我有可以在 3.0+ 设备和 PC 上解码的 JSON 字符串,但在 2.3.x 和可能更低的设备上抛出 JSONException。不过,我很好奇您是如何得出这个答案的?
  • 你知道什么是服务器端解决方案
【解决方案2】:

有类似的问题。起初,我的应用程序在 android 4.0+ 和 4.0-(2.3.3 2.2 等)上运行良好。修改后我有这个问题。 JSONarray 可以在 2.3.3 上解析

问题:Json STRING(来自服务器的响应)前面带有一个字符 ' 所以实际响应= '[{"1":"omg"}] 而不是正确的 [{"1":"omg"}]

解决方案: 如果字符串以 [ 开头,则编辑响应字符串(删除 ' 字符)

  if (result.startsWith("["))
      {



      }
      else
      {
          result= result.substring(1);
      }

在那之后一切对我来说都很好

【讨论】:

  • 这应该更广为人知,也许应该以某种方式解释。在 2.3 中,即使来自 AssetManager 的 JSON 文件也带有此字符,尽管它不在实际文件中。在 4.0+ 上不会发生这种情况。
【解决方案3】:

如果您使用 json-lib-2.4 作为库,我假设,您可以使用:

JSONSerializer.toJSON(yourString).toString() 

而不是使用 JsonObject 类

【讨论】:

  • 它不工作....什么问题我得到响应作为一个字符串但不能把它放入 json obeject 请帮助
  • 如何返回 JSON ?我提供的代码会将一个字符串解析为 JSON 并返回一个 JSON 字符串,无需将字符串放在 JsonObject 中,因为您可以返回它制作的 JSON 字符串?
  • 08-31 15:31:18.821: 错误/AndroidRuntime(1789): 致命异常: main 08-31 15:31:18.821: 错误/AndroidRuntime(1789): java.lang.VerifyError: net.sf.json.JSONSerializer
【解决方案4】:

要删除像 (\n) 这样的字符或 json 字符串中不需要的字符,请在程序中使用 commons-lang3:3.4 库。我使用这个类来删除 json 字符串“StringEscapeUtils.unescapeJava(string)”中不需要的字符。

这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-29
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多