【问题标题】:Retrofit Gson deserialize string special characters改造 Gson 反序列化字符串特殊字符
【发布时间】:2015-10-28 17:40:43
【问题描述】:

我有那个 json

[
  {
    TITLE: "Yoy’s Child"
  },
    TITLE: "Look at me – Stop looking"
  }
]

我使用改造和gson,结果是

Yoy’s Child
Look at me – Stop looking

问题在于对特殊字符进行编码。这是我的代码

GsonBuilder gsonBuilder = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                    return new Date(json.getAsJsonPrimitive().getAsLong());
                }
            });

    gson = gsonBuilder.create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint(WEB_URL)
            .setConverter(new GsonConverter(gson))
            .build();

    apiService = restAdapter.create(APIService.class);

【问题讨论】:

    标签: android json gson retrofit


    【解决方案1】:

    尝试将 disableHtmlEscaping 命令添加到 GsonBuilder 构造函数中。

    GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping();
    

    【讨论】:

    • 还是一样的输出
    【解决方案2】:

    我解决了。我调试了应用程序,发现返回的 json 有符号问题。我所要做的只是将 json 编码为 UTF-8。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多