php如何将转义的json转换成array
06 July 2016
需求如下
将下面的转义后的json转换成数组
源数据为:
{"content":{"data":"\u6709\u94b1\u4e00\u8d77\u8d5a","desc":"\u6709\u94b1\u4e00\u8d77\u8d5a","detail":[{"type":"str","value":"\u6709\u94b1\u4e00\u8d77\u8d5a"}],"type":0,"user":{"id":"@2eb8b02ed214a151722b4ae973896074188ec4ac7f3a2e457cc69de52db4b223","name":"unknown"}},"msg_id":"2665478071232326293","msg_type_id":3,"to_user_id":"@@6a96c35ed848772e8e895b42779a0c610642ec0b5aeae570af05a9d2a11c5b76","user":{"id":"@@6a96c35ed848772e8e895b42779a0c610642ec0b5aeae570af05a9d2a11c5b76","name":"unknown"}}
要求为:
array(5) {
["content"]=>
array(5) {
["data"]=>
string(15) "有钱一起赚"
["desc"]=>
string(15) "有钱一起赚"
["detail"]=>
array(1) {
[0]=>
array(2) {
["type"]=>
string(3) "str"
["value"]=>
string(15) "有钱一起赚"
}
}
["type"]=>
int(0)
["user"]=>
array(2) {
["id"]=>
string(65) "@2eb8b02ed214a151722b4ae973896074188ec4ac7f3a2e457cc69de52db4b223"
["name"]=>
string(7) "unknown"
}
}
["msg_id"]=>
string(19) "2665478071232326293"
["msg_type_id"]=>
int(3)
["to_user_id"]=>
string(66) "@@6a96c35ed848772e8e895b42779a0c610642ec0b5aeae570af05a9d2a11c5b76"
["user"]=>
array(2) {
["id"]=>
string(66) "@@6a96c35ed848772e8e895b42779a0c610642ec0b5aeae570af05a9d2a11c5b76"
["name"]=>
string(7) "unknown"
}
}
完整实现代码如下:
<?php
$str=<<<eof
{"content":{"data":"\u6709\u94b1\u4e00\u8d77\u8d5a","desc":"\u6709\u94b1\u4e00\u8d77\u8d5a","detail":[{"type":"str","value":"\u6709\u94b1\u4e00\u8d77\u8d5a"}],"type":0,"user":{"id":"@2eb8b02ed214a151722b4ae973896074188ec4ac7f3a2e457cc69de52db4b223","name":"unknown"}},"msg_id":"2665478071232326293","msg_type_id":3,"to_user_id":"@@6a96c35ed848772e8e895b42779a0c610642ec0b5aeae570af05a9d2a11c5b76","user":{"id":"@@6a96c35ed848772e8e895b42779a0c610642ec0b5aeae570af05a9d2a11c5b76","name":"unknown"}}
eof;
$htmlspc_str = htmlspecialchars_decode($str, ENT_QUOTES);
$arr = json_decode($htmlspc_str, true);
var_dump ($arr);exit;
?>