简体   繁体   中英

Retrofit Gson deserialize string special characters

I have that json

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

I use retrofit and gson and the results are

Yoy’s Child
Look at me – Stop looking

The problem is encoding that special characters. Here's my code

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);

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

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

I solved it. I debugged the application and found out that the return json had the symbol issues. All I had to do was just to encode json to UTF-8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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