【问题标题】:JsonObject has some extra characterJsonObject 有一些额外的字符
【发布时间】:2014-07-18 16:11:43
【问题描述】:

我在 android 中使用 Volley 库从我的服务器获取 JsonObject。

我已经使用 php 在服务器中创建了正确的 json

但是当我从服务器获取 json 时,会出现一个奇怪的问题

我在php中使用json_encode来生成json

不知道json前面这些多余的字符是什么?

你知道怎么解决这个问题吗???

这是我在android中遇到的错误

07-18 20:40:49.151: W/System.err(11636): com.android.volley.ParseError: org.json.JSONException: Value ï»? java.lang.String 类型的无法转换为 JSONObject

提前致谢

【问题讨论】:

标签: php android json android-volley


【解决方案1】:

我会将此作为评论发布,但我想发布链接中的相关代码。如果它有效,太棒了,如果不让我知道,我会删除它(因为我还没有测试过)。

来自Skip BOM

“问题是您的 UTF-8 字符串以字节顺序标记字符 (BOM) '0xfeff' 开头。我们应该修复我们的 JSON 解析器以跳过该字符(如果存在)。

作为一种解决方法,当您从 InputStream 转到 Reader 时,您可以使用此代码剥离 BOM。"

public Reader inputStreamToReader(InputStream in) throws IOException {
    in.mark(3);
    int byte1 = in.read();
    int byte2 = in.read();
    if (byte1 == 0xFF && byte2 == 0xFE) {
      return new InputStreamReader(in, "UTF-16LE");
    } else if (byte1 == 0xFF && byte2 == 0xFF) {
      return new InputStreamReader(in, "UTF-16BE");
    } else {
      int byte3 = in.read();
      if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF) {
        return new InputStreamReader(in, "UTF-8");
      } else {
        in.reset();
        return new InputStreamReader(in);
      }
    }
  }

“或者您可以从文件中删除字节顺序标记!”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2022-01-09
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2019-03-24
    相关资源
    最近更新 更多