This article mainly introduces the coding conversion problem between json and PHP. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
<br/>
<br/>
<br/>
※Premise: Handling of data conversion situations that may occur when json and php transfer data to each other in the front and backend respectively (mainly for arrays):
<br/>
☆Front-end code:
var arr = {"name":"张伟","age":19}; //此时生成的是json的数组
arr = json.stringify(arr); //此时将arr转换成 json的字符串类型
$.ajax({ type:"post", url:url, data:{arr:arr}, //将转换成字符串类型的json传递给php后台,换换成字符串类型后可以一次性传递多个数据 success:function(res){ console.log(res); } });
<br/>
##☆Backend code (when only passing strings):map = $_POST['arr']; $map = json_decode($map);//json对象 解码之后PHP可以使用,但要用json的方法在后台使用$map->name echo $map->name; // 会输出到res中, 通过consolve.log(res) 可以直接输出
<br/>
<br/>
Question
<br/>##About the situation of passing arrays in php and json, from the background There are two ways to transfer data to the front desk: echo, ajaxreturn
<br/>##$1 The first method:
echo, you need to encode by yourself and output the json string
At this time, there are two situations, one is to return the json string received from the front desk, and the other is to return the json string received from the front desk. The first is to create a new array in the background and return to the front. <br/>
<br/>$1.1 The background receives the foreground data and returns the same data
$map = $_POST['arr'];//从前台传递过来的$map $map = json_decode($map);//json对象 解码之后给php后台使用 ..... $map = json_encode($map);//对json数组进行编码生成json字符串 echo $map; //输出$map到前台
$mapN = json_encode($mapN);//在后台自行创建的数组,在传递给前台之前同样需要进行编码 echo $mapN;
res = eval("("+res+")");//通过res将从后台传过来的json字符串转换成数组 console.log(res.name);
<br/>$2 The second method:
ajaxreturn, no encoding is required when passing the array , the string form of json is passed by default, but eval is required to convert the json string into an array at the front end
<br/>
<br/>
At this time, there are two situations, one is to return the json string received from the front desk, and the other is to create a new array in the background and return to the front desk. <br/>
$2.1 Receive the program from the foreground and return it directly from the background to the foreground<br/>
$map = $_POST['arr']; $map = json_decode($map);//json对象 解码之后 $this->ajaxReturn($map,'json');
$2.2 Create the program in the background and return to the foreground<br/>
$mapN = array("name"=>"zhangwei"); $this->ajaxReturn($mapN,'json');// 此时不需要json_encode() ,前台通过mapN.name 或者mapN['name']输出
$2.3 Front desk operation<br/>
res = eval("("+res+")");//通过res将从后台传过来的json字符串转换成数组 console.log(res.name);
$3 Supplement:
echo and ajaxreturn The difference between passing data from the background to the front:
echo will not encode the php array into a json transfer format - the json string type ajaxreturn will automatically call the json_encode function. EncodingThe same thing between the two:
##$4 Note:
Here the backend can return data to the frontend success: function (res) through echo or ajaxreturn
But the way the data is used after the return is different:
1. String type: through
in the background, you can directly output the string to the res in the front desk,
2. Array type: no matter Is it the string type array type of json passed from the front desk, or the array type generated by the background, which can be returned to the front desk through ajaxreturn:
array name['keyname']
or
method to call
stringify in the front desk). In the background, json_decode is needed to decode and convert it into a json array that can be used by PHP. Calling method Array name->key
2. If the array created in the background is converted into a json string through json_encode, after the data is transmitted to the front desk, it needs to be passed res = eval(" (" res ")"); Convert to json array, front-end calling method:
array name['keyname']or array name.keyname Related recommendations:php character encoding conversion
How to solve garbled characters in php encoding conversion
The above is the detailed content of Encoding conversion problem between json and PHP. For more information, please follow other related articles on the PHP Chinese website!