JSON string deserialized into an object/array method in PHP

Source: Internet
Author: User
This article mainly describes the JSON PHP, JSON string deserialization into an object/array method, has a certain reference value, now share to everyone, the need for friends can refer to

As shown below:

<?php//php parsing JSON information//json_decode (JSON string); $city = Array (' shandong ' = ' Jinan ', ' henan ' = ' zhengzhou ', ' Hebei ' = ' Shijiazhuang '); $jn _city = Json_encode ($city);//anti-coding json$fan_city = Json_decode ($jn _city,false);// The second parameter, FALSE, returns the object type, false to default Var_dump ($fan _city),//object (StdClass) #1 (3) {["Shandong"]=> string (5) "Jinan" ["Henan"]=> string (9) "Zhengzhou" ["Hebei"]=> string "Shijiazhuang"} echo "<br/>"; $fan _city = Json_deco De ($jn _city,true);//The second parameter true returns the array type Var_dump ($fan _city);//array (3) {["Shandong"]=> string (5) "Jinan" ["Henan "]=> string (9)" Zhengzhou "[" Hebei "]=> string ()" Shijiazhuang "}

Manually written JSON strings must be enclosed in single quotation marks to successfully deserialize an object/array:

<?php  //json Information Anti-coding  //different PHP versions, there is a problem with the "pure JSON string" parsing//The JSON string anti-encoding operation using double quotation marks becomes null//$jn = "{' name ': ' Tom ', ' age ' : ' A ', ' addr ': ' Beijing '}; $fan _jn = Json_decode ($jn, true); Var_dump ($fan _jn);//null  //The JSON string anti-encoding operation that is defined with single quotation marks succeeds $jn = ' {' name ': ' Tom ', ' Age ': ', ' ' addr ': ' Beijing '} '; $fan _ JN = Json_decode ($JN, true); Var_dump ($fan _jn);

Related Article

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.