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