开发者

json character encoding problem

When I encode an array to JSON I get开发者_C百科 "u00e1" instead of á.

How could I solve the character encoding?

Thanks


Your input data is not Unicode. 0xE1 is legacy latin1/ISO-8859-*/Windows-1252 for á. \u00e1 is the JSON/JavaScript to encode that. JSON must use a Unicode encoding.

Solve it by either fixing your input or converting it using something like iconv.


The browser's default encoding is probably Unicode UTF-8. Try <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">.


One problem can be if you check the response only (the response is only a text but the JSON must be an object).

You have to parse the response text to be a javascript object first (JSON.parse in javascript) and after that the characters will become the same as on the server side.

Example: On the server in the php code:

$myString = "árvízrtűrő tükörfúrógép";
echo json_encode($myString); //this sends the encoded string via a protocol that maybe    can handle only ascii characters, so the result on the client side is:

On the client side

alert(response); //check the text sent by the php

output: "\u00e1rv\u00edzrt\u0171r\u0151 t\u00fck\u00f6rf\u00far\u00f3g\u00e9p"

Make a js object from the respopnse

parsedResponse = JSON.parse(response);
alert(parsedResponse);

output: "árvízrtűrő tükörfúrógép"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜