时间:2021-07-01 10:21:17 帮助过:14人阅读
$str = '[{"category_name":"类别1","parent_cid":0,"lev":1,"cid":1,"display_order":1},{"category_name":"类别2","parent_cid":0,"lev":1,"cid":2,"display_order":2}]';
$r = json_decode($str);
echo '';print_r($r);
Array
(
[0] => stdClass Object
(
[category_name] => 类别1
[parent_cid] => 0
[lev] => 1
[cid] => 1
[display_order] => 1
)
[1] => stdClass Object
(
[category_name] => 类别2
[parent_cid] => 0
[lev] => 1
[cid] => 2
[display_order] => 2
)
)
------解决方案--------------------
所有編碼都設置為UTF-8就沒啥問題 json只支持UTF-8編碼的中文.
------解决方案--------------------
为什么你的JSON字符串里会有中文?自己拼装的吗?
正常情况下中文应该是编码过的。
如果不能改变文件保存的编码,把中文转码处理下就可以了。
------解决方案--------------------
你的json是自己写的而不是用json_encode生成的吧。
用json_encode生成的会将中文转utf8。
$arr = array (
array (
'category_name' => '类别1',
'parent_cid' => 0,
'lev' => 1,
'cid' => 1,
'display_order' => 1,
),
array (
'category_name' => '类别2',
'parent_cid' => 0,
'lev' => 1,
'cid' => 2,
'display_order' => 2,
)
);
echo json_encode($arr).'
';
echo '';
echo '';'
print_r(json_decode(json_encode($arr),true));
echo '
?>
[{"category_name":"\u7c7b\u522b1","parent_cid":0,"lev":1,"cid":1,"display_order":1},{"category_name":"\u7c7b\u522b2","parent_cid":0,"lev":1,"cid":2,"display_order":2}]
Array
(
[0] => Array
(
[category_name] => 类别1
[parent_cid] => 0
[lev] => 1
[cid] => 1
[display_order] => 1
)
[1] => Array
(
[category_name] => 类别2
[parent_cid] => 0
[lev] => 1
[cid] => 2
[display_order] => 2
)
)