Home  >  Article  >  Backend Development  >  Encoding conversion problem between json and PHP

Encoding conversion problem between json and PHP

不言
不言Original
2018-04-23 11:41:241335browse

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/>


※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):


☆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);
                    
                }
            });


##☆Backend code (when only passing strings):

map = $_POST[&#39;arr&#39;];
            
 $map = json_decode($map);//json对象 解码之后PHP可以使用,但要用json的方法在后台使用$map->name 
 echo $map->name; // 会输出到res中, 通过consolve.log(res) 可以直接输出



Question


##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


##$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.


$1.1 The background receives the foreground data and returns the same data

$map = $_POST[&#39;arr&#39;];//从前台传递过来的$map
$map = json_decode($map);//json对象 解码之后给php后台使用
.....
$map = json_encode($map);//对json数组进行编码生成json字符串
echo $map; //输出$map到前台
$1.2 The background creates the program and returns to the foreground
$mapN = json_encode($mapN);//在后台自行创建的数组,在传递给前台之前同样需要进行编码
echo $mapN;

$1.3 Foreground operation

res = eval("("+res+")");//通过res将从后台传过来的json字符串转换成数组
console.log(res.name);


$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



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.

$2.1 Receive the program from the foreground and return it directly from the background to the foreground

$map = $_POST[&#39;arr&#39;];
$map = json_decode($map);//json对象 解码之后
$this->ajaxReturn($map,&#39;json&#39;);

$2.2 Create the program in the background and return to the foreground

$mapN = array("name"=>"zhangwei");
$this->ajaxReturn($mapN,&#39;json&#39;);// 此时不需要json_encode() ,前台通过mapN.name 或者mapN[&#39;name&#39;]输出

$2.3 Front desk operation

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. Encoding

The same thing between the two:

After passing the json value to the front desk, eval ("(" res ")") is required to convert json array, you can use



##$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

echo

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

array name .keyname

method to call


##Small tips:


1. The front desk passes the json string to the back (itself an array, but it is converted into a json string through

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn