Encoding conversion issues for JSON and PHP

Source: Internet
Author: User
This article mainly introduces the JSON and PHP coding conversion problem, has a certain reference value, now share to everyone, the need for friends can refer to




* Prerequisites: Data conversion processing (mainly for arrays) that may occur when JSON and PHP pass data to each other in the previous background:


☆ Front Code:

var arr = {"name": "Zhang Wei", "Age": 19}; An array of JSON is generated at this time
arr = json.stringify (arr); Convert arr to a JSON string type at this time
$.ajax ({               type: "Post",                Url:url,                Data:{arr:arr},//convert JSON into string type to PHP backend, change to string type to pass multiple data                at once Success:function (res) {                                          console.log (res);                                    }            });


☆ Background code (when passing strings only):

Map = $_post[' arr '];             $map = Json_decode ($map),//json object after decoding PHP can be used, but to use the JSON method in the background using $map->name  echo $map->name;//will be output to the res, Direct output via Consolve.log (res)



Problem


In the case of PHP and JSON passing arrays, there are two ways to pass data from the background to the foreground: Echo,ajaxreturn


The first method:

echo, you need to encode encode your own output JSON string


In this case, there are two cases, one is to return the JSON string received from the foreground, one is to create a new array in the background, and return to the foreground.


$1.1 background receives foreground data and returns the same data

$map = $_post[' arr '];//passed from the foreground $map$map = Json_decode ($map);//json object after decoding to PHP background use ... $map = Json_encode ($MAP);// Encode the JSON array to generate the JSON string echo $map; Output $map to the foreground

$1.2 background creation program, and return to foreground

$mapN = Json_encode ($mapN);//The array created by itself in the background will also need to encode the echo $mapN before being passed to the foreground;

$1.3 Front desk operation

res = eval ("(" +res+ ")");//Convert the JSON string from the background to the array Console.log (res.name) via res;


The second method of $:

Ajaxreturn, it is not necessary to pass an array encode encoding, the default is to pass the JSON string form, but in the foreground requires eval to convert the JSON string into a group



In this case, there are two cases, one is to return the JSON string received from the foreground, one is to create a new array in the background, and return to the foreground.

$2.1 receives the program from the foreground and returns to the foreground directly from the background

$map = $_post[' arr ']; $map = Json_decode ($map);//json object after decoding $this->ajaxreturn ($map, ' json ');


$2.2 background creation program, and return to foreground

$mapN = Array ("name" = "Zhangwei"), $this->ajaxreturn ($mapN, ' json ');//Json_encode () is not required at this time, the foreground is mapn.name or MAPN [' name '] Output

$2.3 Front desk operation

res = eval ("(" +res+ ")");//Convert the JSON string from the background to the array Console.log (res.name) via res;



Add:

Echo and Ajaxreturn the difference between passing data from the background to the foreground:

Echo does not encode the PHP array into a JSON-passed format--json string type

Ajaxreturn will automatically call the Json_encode function to encode


In the same place:

After the JSON value is passed to the foreground, eval ("(" +res+) ") is required to convert to a JSON array before it can be used




$4 Note:

Here backstage through Echo or Ajaxreturn can return data to the foreground of the Success:function (res)


However, the data is used in different ways after the return:

First, the string type: Backstage through echo, you can directly output the string to the foreground res,


Second, array type: Either the JSON string type array type that the foreground passes over, or the array type generated in the background, is returned to the foreground by Ajaxreturn: array name [' KeyName '] or array name. keyname the way to call



Small tips:

One, in the foreground to the back of the JSON string (itself is an array, but in the foreground through the stringify into a JSON string), in the background need to Json_decode to decode, converted to PHP can use the JSON array, call mode Array name->key

Second, the array created in the background, if converted to a JSON string via Json_encode, the data to the foreground, you need to pass res = eval ("(" +res+ ")"); Convert to JSON array, foreground call mode: array name [' KeyName '] or array name. KeyName

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.