The problem with the pit-dad when parsing json in PHP

Source: Internet
Author: User

The AJAX key code is as follows

var myarray={id: "1", type: "New", Status: "Good"};$.post ("test.php", {' Do ': ' Save ', ' MyArray ': Json.stringify (MyArray)} , function (data) {Console.log (data);}); /post mode sends two parameters do and myarray, where MyArray is a serialized JSON string

The key code in PHP is as follows

Receive POST pass parameter $do=$_post[' ID ']; $myArray =$_post[' MyArray '];//parse JSON string $arr=json_decode ($myArray, True); Var_dump ($ ARR);//The result is actually output null

To help the almighty Baidu (Google was wall-linked: P) Some people say it is related to the contenttype set, some people say to file_get_contents ( "php://input" ),按照这两个方向搞了半天,最后还是没搞出来... Use advanced

I then printed the received JSON string without processing it, and found that the string was similar

String ' {\ ' "id\": \ "1\", \ "type\": \ "new\", \ "status\": \ "good\"} '

A comparison of JSON strings generated with the front end through Json.stringify ()

{"Title_1_6": "effect_6,0.6,1.0", "telecom_1_4": "effect_11,0.6,1.0"}

Found many more slashes: \, that is, the system automatically gives the quotes in the standard JSON string passed in the escape processing, so it is not a standard JSON string! Therefore, it needs to be reversed and treated as follows:

$arr =json_decode (stripslashes ($_post[' MyArray "), true)//Remove the slash escape character from the string and successfully

Have a half-day dizzy, originally this is a JSON basic format problem!!!

In addition, the ability to automatically add escape processing to JSON strings in PHP is enabled because the settings

Magic_quotes_gpc=on

The following method can be used to automatically determine whether this setting is turned on and processed accordingly

$json _string = $_post["Json_str"];   if (Ini_get ("magic_quotes_gpc") = = "1") {$json _string=stripslashes ($json _string); } $user = Json_decode ($json _string, true);//true means converting JSON to an array instead of an object

The problem with the pit-dad when parsing json in PHP

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.