0

I have some problems concerning charater encoding with gson. I have a webservice which sends following JSON response:

{
    "version": "v2",
    "result": {
        "class": "SearchSizeBean",
        "result_list": [
            {
                "id": 0,
                "name": "Bitte w\u00c3\u00a4hlen"
            },
            {
                "id": "21",
                "name": "176"
            }
        ]
    }
}

I request the webservice with following code:

ClientResponse response = webResource.path(version)
            .path(path)
            .accept(MediaType.APPLICATION_JSON)
            .method(method.name(), ClientResponse.class, paramMap);

if (response.getStatus() == 200)
{
    responseBody = response.getEntity(String.class);
    ...
}

When checking responseBody the resulting string shows exactly the same result as the JSON mentioned above. But after using GSon to convert the JSON string into an Object, the characters are converted wrong:

Parsing with GSON:

Gson gson = new GsonBuilder().disableHtmlEscaping()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapter(Transferable.class, new TransferableDeserializer())
            .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .create();

RestResponse responseDTO = gson.fromJson(responseBody, RestResponse.class);

Resulting content:

Response: SearchSizeBean[
              resultList={
                  PosSize [id=0, name=Bitte wählen],
                  PosSize [id=21, name=176]
              }
          ]

UPDATE:

I had copied the wrong JSON content. I have edited the JSON to show the correct JSON string. The data is sent in unicode w\u00c3\u00a4. When I check the response after conversion by GSon it becomes hexadecimal: C3 82 C2 A4

4

1 回答 1

1

The problem was on the server side php code. I have a script, which was already encoded in UTF-8. I have encoded the string "Bitte wählen" with the php function utf8_encode() which led to my problem.

Thank you McDowell, your hints brought me on the right track!

The solution was not to use the function and just send the string "Bitte wählen".

于 2013-07-09T15:05:24.563 回答