codeigniter中未定义的常量JSON_UNESCAPED_UNICODE

时间:2014-07-24 04:14:35

标签: php json codeigniter

我创建了一个Json文件

$json = array(
    "Add"=>array(
    "context" => "$context",
    "date"=> "$date"
    ),
    "Version"=>array(
    "version"=>$version
    )
);

$context是一个波斯文本(因此我的Json需要UTF-8格式)。现在我用这个

echo json_encode($json,JSON_UNESCAPED_UNICODE);

查看我的json文件,但Codeigniter给出了这两个错误

A PHP Error was encountered

Severity: Notice

Message: Use of undefined constant JSON_UNESCAPED_UNICODE - assumed 'JSON_UNESCAPED_UNICODE'

A PHP Error was encountered

Severity: Warning

Message: json_encode() expects parameter 2 to be long, string given

有什么想法吗?先谢谢。

4 个答案:

答案 0 :(得分:4)

来自the documentation

  

JSON_UNESCAPED_UNICODE(整数)

     

[...]自PHP 5.4.0起可用。

您的PHP版本没有定义常量(并定义它不会有帮助)。

答案 1 :(得分:1)

也许这会对你有所帮助:

function json_cb(&$item, $key) { 
    if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); 
}

function my_json_encode($arr){
    //convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding
    array_walk_recursive($arr, 'json_cb');
    return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');

}

尝试my_json_encode而不是json_encode。

答案 2 :(得分:0)

在较新的PHP版本上,此错误是由缺少php-json模块引起的。只需安装它,您应该就很好了。例如在CentOS中:sudo dnf install php-json

答案 3 :(得分:-1)

function json_encode_unicode($data) {
if (defined('JSON_UNESCAPED_UNICODE')) {
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
return preg_replace_callback('/(?<!\\\\)\\\\u([0-9a-f]{4})/i',
function ($m) {
$d = pack("H*", $m[1]);
$r = mb_convert_encoding($d, "UTF8", "UTF-16BE");
return $r!=="?" && $r!=="" ? $r : $m[0];
}, json_encode($data)
);
}
// some benchmarking...
print "\n\n".title("Benchmarks")."\n";
gauge("json_encode", 1000, function() use ($data) {
json_encode($data);
});
gauge("json_encode_unicode", 1000, function() use ($data) {
json_encode_unicode($data);
});
// Utils...
function gauge($label, $times, $callback) {
$tstart = microtime(true);
for ($i=0; $i<$times; $i++) {
$callback();
}
$tend = microtime(true);
$took = ($tend-$tstart);
print (ceil($took*1000)/1000)." to run $label run $times times\n";
}
function title($str) {
return $str."\n".str_repeat("-",strlen($str));
}
?>

result

php: print_r(data)
------------------
stdClass Object
(
[html] => <foo bar="baz"/> &amp;
[arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī
[hebrew] => עִבְרִית, Ivrit
[chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén
[korean] => 한국어/조선말
[japanese] => 日本語 Nihongo
[umlauts] => äüöãáàß
[escaped] => \u65e5\u672c\u8a9e
[emoji] => ☺ <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/263a.png' class='emoji' title='WHITE SMILING FACE' alt='WHITE SMILING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' >
)


php: json_encode(data)
----------------------
{"html":"<foo bar=\"baz\"\/> &amp;","arabic":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629 al-\u02bfarabiyyah, IPA: [\u00e6l \u0295\u0251r\u0251\u02c8bijj\u0250], or \u0639\u0631\u0628\u064a \u02bfarab\u012b","hebrew":"\u05e2\u05b4\u05d1\u05b0\u05e8\u05b4\u05d9\u05ea, Ivrit","chinese":"\u6c49\u8bed\/\u6f22\u8a9e Hanyu; \u534e\u8bed\/\u83ef\u8a9e Hu\u00e1y\u01d4; \u4e2d\u6587 Zh\u014dngw\u00e9n","korean":"\ud55c\uad6d\uc5b4\/\uc870\uc120\ub9d0","japanese":"\u65e5\u672c\u8a9e Nihongo","umlauts":"\u00e4\u00fc\u00f6\u00e3\u00e1\u00e0\u00df","escaped":"\\u65e5\\u672c\\u8a9e","emoji":"\u263a \ue415\ue056\ue057\ue414\ue405\ue106\ue418 \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"}
strlen: 708


php: json_encode_unicode(data)
------------------------------
{"html":"<foo bar=\"baz\"\/> &amp;","arabic":"العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī","hebrew":"עִבְרִית, Ivrit","chinese":"汉语\/漢語 Hanyu; 华语\/華語 Huáyǔ; 中文 Zhōngwén","korean":"한국어\/조선말","japanese":"日本語 Nihongo","umlauts":"äüöãáàß","escaped":"\\u65e5\\u672c\\u8a9e","emoji":"☺ <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/263a.png' class='emoji' title='WHITE SMILING FACE' alt='WHITE SMILING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > \ud83d\ude04\ud83d\ude0a\ud83d\ude03\ud83d\ude09\ud83d\ude0d\ud83d\ude18"}
strlen: 471


php: print_r(json_decode(json_encode_unicode(data)))
----------------------------------------------------
stdClass Object
(
[html] => <foo bar="baz"/> &amp;
[arabic] => العربية al-ʿarabiyyah, IPA: [æl ʕɑrɑˈbijjɐ], or عربي ʿarabī
[hebrew] => עִבְרִית, Ivrit
[chinese] => 汉语/漢語 Hanyu; 华语/華語 Huáyǔ; 中文 Zhōngwén
[korean] => 한국어/조선말
[japanese] => 日本語 Nihongo
[umlauts] => äüöãáàß
[escaped] => \u65e5\u672c\u8a9e
[emoji] => ☺ <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/263a.png' class='emoji' title='WHITE SMILING FACE' alt='WHITE SMILING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' > <img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f604.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' alt='SMILING FACE WITH OPEN MOUTH AND SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60a.png' class='emoji' title='SMILING FACE WITH SMILING EYES' alt='SMILING FACE WITH SMILING EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f603.png' class='emoji' title='SMILING FACE WITH OPEN MOUTH' alt='SMILING FACE WITH OPEN MOUTH' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f609.png' class='emoji' title='WINKING FACE' alt='WINKING FACE' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f60d.png' class='emoji' title='SMILING FACE WITH HEART-SHAPED EYES' alt='SMILING FACE WITH HEART-SHAPED EYES' style='height:1em !important; width:1em !important; float:none !important' ><img src='chrome-extension://cahedbegdkagmcjfolhdlechbkeaieki/images/1f618.png' class='emoji' title='FACE THROWING A KISS' alt='FACE THROWING A KISS' style='height:1em !important; width:1em !important; float:none !important' >
)


php: data == json_decode(json_encode_unicode(data))
---------------------------------------------------
1



Benchmarks
----------
0.01 to run json_encode run 1000 times
0.658 to run json_encode_unicode run 1000 times