views:

301

answers:

2

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

<?php 
 $result = array('wine'=>"测试", 'عربي'=>4, 'lemon'=>22);
 echo  json_encode($result);

and i also tried Zend_JSON Library ,

<?php 
 require_once 'Zend/Json.php';
 $result = array('wine'=>"测试", 'عربي'=>4, 'lemon'=>22);
 echo Zend_Json::encode($result);

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  A: 

What exactly is crazy about the output?

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

That code seems to successfully show عربي

Clyde
I wasn't know that unicode notation understood by javascript/ecmascript like this i just was expecting the same string since i usually work with it in English .Thanks !
tawfekov
+1  A: 

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.
VolkerK
Thanks VolkerK , You explain it very well !
tawfekov