Json is a data format of js and can be directly parsed by js. php cannot directly read json data, but php provides the json_decode function to convert json data, this can be accessed by php scripts. Today, the Webmaster will discuss this issue with you. If you are interested, refer to it.
Json is a data format of js and can be directly parsed by js. php cannot directly read json data, but php provides the json_decode function to convert json data, this can be accessed by php scripts. Today, the Webmaster will discuss this issue with you. If you are interested, refer to it.
Json is often used when php and javascript are used to implement some functions. Json is a data format of js and can be directly parsed by js. Php cannot directly read json data, but php provides the json_decode function to convert json data, which can be accessed by php scripts. Php also provides the json_encode function to convert data into json format. Is the native json in js exactly the same as the json converted by using the json_encode function in php? Today, the Webmaster will discuss this issue with everyone.
When we use php to pass array data to javascript, we usually need to convert it to json format and retrieve it again using javascript. So let's take the array as an example. Let's first look at the difference between the two.
1. One-dimensional array
Php Array
The Code is as follows:
$ Array = array ("1", "2", "3 ");
After conversion using the json_encode function, the corresponding json string is
The Code is as follows:
["1", "2", "3"].
Careful friends soon discovered that the converted json string is in the array form of javascript. can I access it using the array access method of js?
Yes, of course, but when you pass this json string to js, you need to use the urlencode function to encode it, such:
The Code is as follows:
') "Id =" aj "> access json
We can use the following js Code for verification:
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 by accessing the one-dimensional array in js. The eval method interprets the json string as a json object, because it is passed as a string. If it is not converted, you will get the value of the third character in the string.
Let's make a change to this one-dimensional array. We find that the one-dimensional array above does not specify an index, so it is a digital index by default. Now let's add a key name to it:
Php Array
The Code is as follows:
$ Array = array ('A' => '1', 'B' => '2', 'c' => '3 ');
After conversion using the json_encode function, the corresponding json string is
The Code is as follows:
{"A": "1", "B": "2", "c": "3 "}
.
We soon discovered the difference. The most obvious difference is that the [] at both ends of the string is changed to {}. Can this string be accessed by js after being processed as above? Let's try it out:
The Code is as follows:
'1', 'B' => '2', 'c' => '3');?> ') "Id =" aj "> access json
Function show (str ){
Var jobj = eval_r (decodeURI (str ));
Alert (jobj. );
}
If you try it, you will know that the pop-up window does not appear after you click the link. Why? Is the json string format generated by PHP incorrect? No, this is an error when we use the eval function to explain it. Replace the above function code:
The Code is as follows:
Function show (str ){
Var jobj = eval_r ('+ decodeURI (str) + ')');
Alert (jobj. );
}
Try again! You can access it. This tells us that when the eval method is used to process a json string with a key name, brackets must be accelerated at both ends of the string. As for why, the webmaster does not know, standing on the shoulders of giants.
Note that although the json string generated by PHP
{"A": "1", "B": "2", "c": "3"} cannot be directly interpreted as json format after being passed to js, however, if you use this string in js to directly create json data, you can. Try the following code:
The Code is as follows:
Var jobj = {"a": "1", "B": "2", "c": "3 "};
Alert (jobj. B );
2. Two-dimensional array
2D arrays are widely used in PHP. Therefore, it is very important to understand the json format after 2D arrays are converted. With the above example, the Webmaster will directly give the sample code:
The Code is as follows:
') "Id =" aj "> access json
Function show (str ){
Var jobj = eval_r (decodeURI (str ));
Alert (jobj [0] [0]);
}
Run the command and you will find that this is similar to a one-dimensional array. This is an example without a key name. Therefore, in the show function, remove the brackets at both ends of the string.
Next, let's change the two-dimensional array and add the key name in the second dimension. See the sample code:
The Code is as follows:
'1', "B" => '2', '3');?> ') "Id =" aj "> access json
Function show (str ){
Var jobj = eval_r ('+ decodeURI (str) + ')');
Alert (jobj [0]. );
}
After running the code, we found that the method for accessing json data here is a bit different. We use
Alert (jobj [0] [0]);
Here we use
Alert (jobj [0]. a); don't ask me why, that's it. This is the json access method.
In the above example, we add a key name to the second-dimensional array. Next we add a key name to the First-dimensional array to see how the access method is different:
The Code is as follows:
Array ('1', '2', '3');?> ') "Id =" aj "> access json
Function show (str ){
Var jobj = eval_r ('+ decodeURI (str) + ')');
Alert (jobj. k [1]);
}
Here we use
Jobj. k [1], as long as the array contains a key name, when the array is converted to json format, you must use
Json object. Key name
In this way, access the elements under the key. In the above example, the array elements under the k key are numerical indexes, therefore, use k [1] to access json.
Next, we add a key name to both the first and second dimensions of the array:
The Code is as follows:
Array ("a" => '1', '2', '3');?> ') "Id =" aj "> access json
Function show (str ){
Var jobj = eval_r ('+ decodeURI (str) + ')');
Alert (jobj. k. );
}
As mentioned above, as long as the key name is included
Json object. Key name
If there are multiple keys, use
Json object. Key name. Key name...
Don't ask me why. This is the json access method. Only the inventor of javascript can explain to you why he wants to do so.
Conclusion:
1. When converting an 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 js eval method to convert it to json format for js processing. If the array contains a key name, when you use the eval method for processing, need to use
()
Enclose json strings.
2. If the array contains a key name and is converted to a json string, use
Json object. Key name. Key name...
In the format
Json object [1]
Or
Json object. Key name [1]
In this way.
As mentioned above, we mainly discuss things to be aware of when PHP transmits a json string to js. Next we will discuss how to handle json strings transmitted to php using js.
You can use the json_decode function to decode json data by referencing json data as a string. That's right! That's it! However, you must be careful when constructing a json string. If you do not frequently construct a json string, use
Echo json_encode (array ('K' => array ("a" => '1', '2', '3 ')))
In this way, you can view the json format of the target string you want to construct. In this way, you can construct it in js Based on the desired results!
Now, we will discuss how to use json data for communication between php and js. You can try to encode php Object Types in json format and then transfer them to js.