How to convert UTF-8 text into JSON format

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

I was working on project that had problem when encoding UFT-8 text into JSON Format

and i also tried Zend_JSON Library ,

in both cases the JSON output was crazy :

{"wine":"\u0639\u0631\u0628\u064a ","\u0639\u0631\u0628\u064a":4,"lemon":22} 

i did try it into PHP5.2.6 and PHP5.3 but same result ..

How do I convert UTF-8 text into JSON? Any suggestions?

回答1:

That's a unicode notation understood by javascript/ecmascript. Try

<html>   <head>     <title>unicode test</title>     <script type="text/javascript">       function foo() {         var outDiv = document.getElementById("bar");         var jsondata = {"wine":"\u0639\u0631\u0628\u064a ","\u0639\u0631\u0628\u064a":4,"lemon":22};           for ( var k in jsondata ) {           outDiv.innerHTML += k + "=" + jsondata[k] + "<br />";         }       }     </script>     </head>   <body onload="foo()">     <div id="bar"></div>   </body> </html> 

to see for yourself.

http://www.ecmascript.org/docs/tc39-2009-043.pdf (page 14) :

In string literals, regular expression literals, and identifiers, any character (code unit) may also be expressed as a Unicode escape sequence consisting of six characters, namely \u plus four hexadecimal digits.


回答2:

What exactly is crazy about the output?

var b = {"wine":"\u0639\u0631\u0628\u064a ", test:"\u0639\u0631\u0628\u064a","lemon":22}; alert (b.wine); 



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!