Discussion on how php and js transfer data to each other through json

高洛峰
Release: 2023-03-02 19:18:02
Original
1131 people have browsed it

When we combine php and javascript to implement certain functions, json is often used. json is a data format of js and can be parsed directly by js. PHP cannot read json data directly, but PHP provides the json_decode function to convert json data so that it can be accessed by PHP scripts. At the same time, PHP also provides the json_encode function to convert data into json format. So, are the native json in js exactly the same as the json converted by the json_encode function in php? Today, the webmaster will discuss this issue with everyone.

When we pass array data to javascript through php, we usually need to convert it into json format and use javascript to obtain it. Let's take the array as an example and first look at the difference between the two.

1. One-dimensional array
Consider PHP array
Copy code The code is as follows:
$array=array("1","2","3");

After conversion using the json_encode function, the corresponding json string is
Copy the code The code is as follows:
["1","2","3"].

Careful friends soon discovered that the json string obtained after conversion is in the form of an array in javascript, so can it be accessed using the js array access method?
Of course it is possible, but when you pass this json string to js, ​​you need to use the urlencode function to encode it, such as:
Copy code The code is as follows:

We can use the following js code to verify:
Copy code The code is as follows:
function show(str){
var jobj=eval_r(decodeURI(str));
alert(jobj[2]);
}

If you try it yourself, you will find that yes, you can access it in the same way as accessing a one-dimensional array in js. The eval method interprets the json string as a json object, because what is passed is a string. If it is not converted, you will get the value of the third character in the string.
Let’s make some changes to this one-dimensional array. We find that the one-dimensional array above does not have an index specified, so it defaults to a numeric index. Now let’s add a key name to it:
Consider a php array
Copy the code The code is as follows:
$array=array('a'=>'1','b'=>'2','c'=>'3');

The corresponding json string after conversion using the json_encode function Copy the code for
The code is as follows:
{"a":"1","b":"2","c":"3"}

.
We quickly discovered the difference. The most obvious one is that the [] at both ends of the string became {}. So can this string be processed as above and accessed by js? Let’s give it a try:
Copy the code The code is as follows:
function show(str){
var jobj=eval_r(decodeURI(str)) ;
alert(jobj.a);
}

If you try it, you will know that after clicking the link, no pop-up window appears. why? Is the json string format generated by PHP wrong? No, this is when we used the eval function to explain, an error occurred. Replace the above function code with:
Copy code The code is as follows:
function show(str){
var jobj=eval_r('('+decodeURI(str)+')');
alert(jobj.a);
}

Try again! How about it? You can visit now. This tells us that when using the eval method to process a json string with key names, you need to accelerate the brackets at both ends of the string. As for why, the webmaster doesn’t know. He is just standing on the shoulders of giants.
It should be noted here that although the json string generated by PHP
{"a":"1","b":"2","c":"3"} cannot be directly interpreted into json format after being passed to js , but if you use this string to directly create json data in js, it is ok. Try the following code:
Copy code The code is as follows:
var jobj={"a":"1","b":"2","c":"3"};
alert(jobj.b) ;

2. Two-dimensional array
Two-dimensional arrays are widely used in PHP, so it is very important to understand the json format after the two-dimensional array is converted. With the above example as a basis, the webmaster will directly give the sample code below:
Copy the code The code is as follows:
function show(str){
var jobj=eval_r(decodeURI(str));
alert(jobj[0][0]);
}

Everyone will find that this is similar to a one-dimensional array. This is an example without a key name. Therefore, in the show function, it is also possible to remove the brackets at both ends of the string.
Next, we make some changes to the two-dimensional array and add key names to the second dimension. Please see the sample code:
Copy the code The code is as follows:
function show(str){
var jobj=eval_r('('+decodeURI(str)+')');
alert(jobj[0].a);
}

After everyone ran the code, they found that the way we access json data here is a bit different. Same. Above we used
alert(jobj[0][0]);
And here we used
alert(jobj[0].a); Don’t ask me why, that’s it. This is how json is accessed.
In the above example, we added a key name to the second dimension of the two-dimensional array. Next, we add a key name to the first dimension to see what the access methods are different:
Copy the code The code is as follows:
function show(str){
var jobj=eval_r('('+decodeURI(str)+')');
alert(jobj.k[1]) ;
}

Here we are using the method of
jobj.k[1]. You must have discovered that as long as the array contains key names, when the array is converted to json format, the
json object must be used. The key name
is used to access the elements under the key. In the above example, the array element under the k key is a numeric index, so use k[1] to access it in json.
Next, we add key names to both the first and second dimensions of the array:
Copy the code The code is as follows:
function show(str){
var jobj=eval_r('('+decodeURI(str)+')');
alert(jobj.k.a);
}

As mentioned above, as long as it contains a key name, it must be a
json object .Key name
to access, if there are multiple keys, use
json object.Key name.Key name...
, don’t ask me why, this is the access method of json, only the inventor of javascript can tell Please explain why he stipulates this.
Conclusion:
1. When converting the array in php into a json string and passing it to js. If the array does not specify a key name, you can directly use the eval method of js to convert it into json format for js processing; if the array contains a key name, then when using the eval method to process it, you need to use
()
to convert the json string bracketed.
2. If the array contains a key name, after converting it into a json string, use
json object.Key name.Key name...
to access it in js. If it is a numerical index, use
json object[ 1]
or
json object.Key name[1]
This way.
Above, we mainly discussed the things that need to be paid attention to when passing json strings to js in PHP. Next we will discuss how to process the json string when passing it to php using js.
If you are smart, you must already know that as long as the json data is enclosed in quotes and passed as a string to PHP [usually using ajax], you can use the json_decode function to decode it. That's right! That's it! But you must be careful when constructing json strings. If you don't often construct json strings, you might as well use
echo json_encode(array('k'=>array("a"=>'1','2 ','3')))
In this way, view the json format of the target string you need to construct. In this way you can construct it in js according to the results you want!
Okay, today we have discussed how to use json data to communicate between php and js. You can try to pass the object type of php to js after json encoding.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template