How do php and js transfer data to each other through json

  • 2020-05-27 04:40:23
  • OfStack

json is often used when combining php and javascript to achieve certain functions. json is a data format of js, which can be directly parsed by js. While php cannot read json data directly, 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 to json format. Then, is the original json in js and the json in php completely the same after conversion by json_encode function? Today, stationmaster and everybody 1 get up to discuss this problem.

When we pass array data from php to javascript, we usually convert it to json format and get it once through javascript. So let's take an array as an example and look at the difference between the two.

1, 1 - dimensional array
Consider the php array
 
$array=array("1","2","3"); 

After converting using the json_encode function, the corresponding json string is
 
["1","2","3"] .  

Careful friends will soon find that the converted json string is in the form of an array in javascript, so can you use the array access method of js to access it?
Of course you can, but when you pass the json string to js, you need to use the urlencode function to encode it, such as:
 
<a href="javascript:show('<?php echo urlencode(json_encode(array('1','2','3')));?>')" id="aj"> access json</a> 

We can verify this with the js code below:
 
function show(str){ 
var jobj=eval_r(decodeURI(str)); 
alert(jobj[2]); 
} 

If you try 1 for yourself, you'll see that, yes, you can access it in the same way that you would access a 1-dimensional array in js. The eval method interprets the json string as an json object, because you're passing in a string, and if you don't convert, you get the value of the third character in the string.
Let's make another change to this 1-dimensional array. We find that the 1-dimensional array above has no index specified, so it is numeric index by default. Now let's add the key name to it:
Consider the php array
 
$array=array('a'=>'1','b'=>'2','c'=>'3'); 

After using the json_encode function, the corresponding json string is
 
{"a":"1","b":"2","c":"3"} 

.
The most obvious difference is that the [] at both ends of the string becomes {}. Can this string be accessed by js as above? We don't want to try 1:
 
<a href="javascript:show('<?php echo urlencode(json_encode(array('a'=>'1','b'=>'2','c'=>'3')));?>')" id="aj"> access json</a> 
function show(str){ 
var jobj=eval_r(decodeURI(str)); 
alert(jobj.a); 
} 

If you try it out, there's no popover when you click on the link. Why is that? Is the json string generated by PHP in the wrong format? No, it was an error when we were using the eval function. Replace the above function code with:
 
function show(str){ 
var jobj=eval_r('('+decodeURI(str)+')'); 
alert(jobj.a); 
} 

Try again! Well, you're ready to visit. This tells us that when you use the eval method for json strings with key names, you need to speed up the parentheses at both ends of the string. As to why, stationmaster also does not know, stand on the giant's shoulder only.
Note here that although PHP generates json strings
{"a":"1","b":"2","c":"3"} cannot be directly interpreted as json after being passed to js, but if you use this string in js to create json data directly, you can. Try the following code:
 
var jobj={"a":"1","b":"2","c":"3"}; 
alert(jobj.b); 

2, 2 dimensional array
Two-dimensional arrays are widely used in PHP, so it is important to understand the converted json format. With the above example to pave the way, the following webmaster will directly give the sample code:
 
<a href="javascript:show('<?php echo urlencode(json_encode(array(array('1','2','3'))));?>')" id="aj"> access json</a> 
function show(str){ 
var jobj=eval_r(decodeURI(str)); 
alert(jobj[0][0]); 
} 

If you run it, you'll see that this is similar to a 1-dimensional array, and this is an example without a key name, so in the show function, you can remove the parentheses from both ends of the string.
Next, we will make a 1 change to the 2-dimensional array and add the key name in the 2nd dimension. Please see the sample code:
 
<a href="javascript:show('<?php echo urlencode(json_encode(array(array("a"=>'1',"b"=>'2','3'))));?>')" id="aj"> access json</a> 
function show(str){ 
var jobj=eval_r('('+decodeURI(str)+')'); 
alert(jobj[0].a); 
} 

When you run the code, you'll notice that the way we're accessing the json data is a little bit different. So up here we're using theta
alert(jobj[0][0]);
And here we're going to use theta
alert (jobj [0]. a); Don't ask me why, that's it. This is how json is accessed.
In the above example, we added the key name to the second dimension of the 2-d array. Now we add the key name to the first dimension to see how the access is different:
 
<a href="javascript:show('<?php echo urlencode(json_encode(array('k'=>array('1','2','3'))));?>')" id="aj"> access json</a> 
function show(str){ 
var jobj=eval_r('('+decodeURI(str)+')'); 
alert(jobj.k[1]); 
} 

Here we're using theta
jobj.k [1], as you have already discovered, is used when the array is converted to json, as long as the array contains the key name
json object. Key name
In the above example, the array elements under the k key are numeric indexes, so k[1] is used to access them in json.
Next, we add key names to both the 1st and 2nd dimensions of the array:
 
<a href="javascript:show('<?php echo urlencode(json_encode(array('k'=>array("a"=>'1','2','3'))));?>')" id="aj"> access json</a> 
function show(str){ 
var jobj=eval_r('('+decodeURI(str)+')'); 
alert(jobj.k.a); 
} 

As mentioned above, as long as the key name is included, it must be
json object. Key name
If there are multiple keys to use
json object. Key name. Key name...
Don't ask me why. That's how json is accessed. Only the inventor of javascript can explain to you why he did it.
Conclusion:
1. When the array in php is converted into json string and passed to js. If the array does not specify a key name, you can directly convert it to json for js processing using js's eval method. If the array contains the key name, you need to use it when using the eval method
()
Enclose the json string.
2. If the array contains the key name, convert it to json string and use it in js
json object. Key name. Key name...
If it is a numeric index, use
json object [1]
or
json object. Key name [1]
This way.
Above, we focused on the considerations when passing an json string from PHP to js. Let's talk about what you need to do when you pass an json string from js to php.
If you're smart enough to know this already, you can decode the json_decode function by simply passing json data as a string in quotes to PHP (usually ajax). That's right! That's it! But be careful when constructing json strings. If you don't often construct json strings, use it
echo json_encode(array('k'= > array("a"= > '1','2','3')))
This way, look at the json format of the target string you need to construct. So you can construct it in js according to the results you want!
Ok, so that's the end of today's discussion of how php and js communicate using json data. You can also try json encoding the object types of php and pass them to js.

Related articles: