在json解码中格式化

时间:2013-11-30 07:49:40

标签: php json

我对解码有疑问 translatsions.php就像

<?php
    define("LNG_T1","sdfjaö");
    define("LNG_T2","õäöüåÕÄÖÜ");
    define("LNG_T3","Русский");
    define("LNG_T4","Українська");
    define("LNG_T5","日本語");
    define("LNG_T6","العربية");
?>

还有另一个php文件

<?php

    function startsWith($haystack, $needle) {
        return !strncmp($haystack, $needle, strlen($needle));
    }
    require_once 'translatsions.php';
    header('Content-type: text/javascript; charset=UTF-8');

    $res = array();
    foreach (get_defined_constants() as $k => $v) {
        if (startsWith($k, "LNG_")) {
            $v = preg_replace('/\'/', '&#39;', $v);
            $k = strtolower(substr($k, 4));
            $res[$k] = $v;
        }
    }

?>

echo json_encode($ res,JSON_PRETTY_PRINT) 我想看看json

{
    "t1":"sdfjaö",
    "t2":"õäöüåÕÄÖÜ",
    "t3":"Русский",
    "t4":"Українська",
    "t5":"日本語",
    "t6":"العربية"
}

不像json那样:

{
    "t1": "sdfja\u00f6",
    "t2": "\u00f5\u00e4\u00f6\u00fc\u00e5\u00d5\u00c4\u00d6\u00dc",
    "t3": "\u0420\u0443\u0441\u0441\u043a\u0438\u0439",
    "t4": "\u0423\u043a\u0440\u0430\u0457\u043d\u0441\u044c\u043a\u0430",
    "t5": "\u65e5\u672c\u8a9e",
    "t6": "\u0627\u0644\u0639\u0631\u0628\u064a\u0629"
}

PHP有这样的通用功能吗?

1 个答案:

答案 0 :(得分:4)

使用JSON_UNESCAPED_UNICODE选项以及JSON_PRETTY_PRINT