How to transmit data through json in php and js

Source: Internet
Author: User
How php and js transmit data through json. 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 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.

Bytes. Json is a data format of js and can be directly parsed by js. Php cannot directly read json data,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.