Question about json_decode Chinese! There are the following strings: [{category_name: Category 1, parent_cid: 0, lev: 1, cid: 1, display_order: 1}, {category_name: Category 2, parent_cid: 0, the following is a question about how to use json _ json_decode in Chinese: 1, cid: 2, display_order: 2!
There are the following strings:
[{"Category_name": "Category 1", "parent_cid": 0, "lev": 1," cid ": 1," display_order ": 1 }, {"category_name": "Category 2", "parent_cid": 0, "lev": 1," cid ": 2," display_order ": 2}]
The return value is null when json_decode is converted to an array. check that json_decode does not support Chinese characters.
How can we convert it into an array?
I can't find many methods on the Internet.
Which one provides an effective solution. (File encoding cannot be changed)
Thank you.
------ Solution --------------------
Where is your string from? Is the clip input obtained from other places?
------ Solution --------------------
Php json seems to only support UTF-8. use iconv for other encoding.
------ Solution --------------------
$ Str = '[{"category_name": "Category 1", "parent_cid": 0, "column": 1, "cid": 1, "display_order": 1 }, {"category_name": "Category 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] => Category 1
[Parent_cid] => 0
[EV] => 1
[Cid] => 1
[Display_order] => 1
)
[1] => stdClass Object
(
[Category_name] => Category 2
[Parent_cid] => 0
[EV] => 1
[Cid] => 2
[Display_order] => 2
)
)
------ Solution --------------------
All zookeeper is set to UTF-8, so there is no problem. json only supports Chinese characters of UTF-8 zookeeper.
------ Solution --------------------
Why does your JSON string contain Chinese characters? Self-assembled?
Normally, Chinese characters are encoded.
If you cannot change the encoding of the file, you can transcode the Chinese file.
------ Solution --------------------
Instead of using json_encode to generate your own json.
If json_encode is used, Chinese characters are converted to utf8.
$ Arr = array (
Array (
'Category _ name' => 'Category 1 ',
'Parent _ Cid' => 0,
'Lev' => 1,
'CID' => 1,
'Display _ order' => 1,
),
Array (
'Category _ name' => 'category 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] => Category 1
[Parent_cid] => 0
[EV] => 1
[Cid] => 1
[Display_order] => 1
)
[1] => Array
(
[Category_name] => Category 2
[Parent_cid] => 0
[EV] => 1
[Cid] => 2
[Display_order] => 2
)
)