Question about json_decode

Source: Internet
Author: User
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
)

)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.