Unicode Json Encode issue in PHP

in my database, it has some unicode character and when I :

$result=mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
$response[] = $row;
}
echo json_encode($response);

El resultado es

[{
    "id": "151",
    "titlenews": "iPad2 ? iPhone Mini ????????????? ???????????????? Samsung Galaxy Tab",
    "photo": "ipad_1294990020.jpg"
}, {
    "id": "153",
    "titlenews": "???????????????????????? iPhone 5 ??????????????????? Apple",
    "photo": "iphone-5_1294996201.jpg"
}, {
    "id": "154",
    "titlenews": "Android ??????????????????????????????????? ????????????????????????????????????????????????????? 2010",
    "photo": "android_1295234852.jpg"
}]

Why it has "?", it should encode to any special character represent the unicode.

Agradezcan

preguntado el 31 de julio de 12 a las 14:07

Did you send the appropriate charset headers? -

Passed through jsbeautifier.org to make it easier to read... -

sorry i don't really know about charset headers, what is that ? do we need to add sth more? thx -

@sayvortana: mysql_set_charset. Also: the mysql extension is deprecated. Use mysqli or PDO instead. -

2 Respuestas

Esto es lo que uso

function myencode(&$item,$key)
{
    $item=utf8_encode($item);
}

function my_json_encode($arr)
{
    if(is_array($arr))
    {
        array_walk_recursive($arr,myencode);
        return json_encode($arr);
    }
    else
    {
        return json_encode(array());
    }
}

And then to decode

function my_json_decode($json)
{
    return json_decode(safeJSON_chars(stripslashes($json)),TRUE);
}

Respondido 31 Jul 12, 15:07

For my problem, as @jon commented I need to add mysql_set_charset("utf8", $con) and now it works fine - dicevortana

Solution in comments (by Savartona) :

mysql_set_charset("utf8", $con);

Respondido 31 Jul 12, 15:07

-1 because: a) He wants to output JSON, so obviously he cannot use a <meta> tag (an HTTP header might do the trick); b) There is absolutely no indication that the problem is at the client; c) Even if none of this were true, 8859-1 would not be the correct charset because json_encode returns UTF-8. - Jon

By add mysql_set_charset("utf8", $con) now it works fine thank guys, a - dicevortana

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.