PHP app JSON tip _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP app JSON skills. Phpjson_decode returns the data. js processing phpjson_decode. the data returned to the foreground is: encode_str {green: 10, size: 5, strock: 12}. then js uses eval (obj + encode_str + Processing of js returned data by php json_decode

After php json_decode, the data returned to the front-end is: encode_str = "{" green ": 10," size ": 5," strock ": 12}
Then js uses eval ("obj =" + encode_str + ";");
Json data can be instantiated as an object, and data can be obtained directly by obj. green.

In Javascript, {} can be used to represent an object, and [] can be used to represent an array, for example:

Var obj = {"a": "v", "B": "x"}; // This indicates that the variable obj is an object and has two attributes: a and B, the attribute values are v and x.
Var arr = ["v", "x"]; // This indicates that the arr variable is an array with two elements. the indexes are 0 and 1, and the values are: v and x.
JSON is the format of the logical structure of the data in which the two formats are mixed. In fact, JSON is a mixture of objects and arrays in Javascript.

PHP provides specialized functions to generate and parse JSON-format data. The data parsed by PHP has the same meaning as the original Javascript data, that is, the Javascript object is parsed into a PHP object, the Javascript array is parsed into a PHP array. The JSON function used by PHP is json_encode ($ PHPcode );
The JSON parsing function of PHP is: json_decode ($ JSONcode );

Therefore, there are multiple JSON forms. different forms are different after PHP explains them.

The code is as follows:


// Form 1: completely in the form of an object. in this form, data is stored in Javascript
Is also called the related array, which is different from the general array,
It can be accessed through string indexing (using "[]" or "."
To indicate the level)
$ Json = '{"item1": {"item11": {"n": "chenling ",
"M": "llll"}, "sex": "male", "age": "25"}, "item2 ":
{"Item21": "ling", "sex": "female", "age": "24 "}}';
$ J = json_decode ($ json );
Print_r ($ J );


Output:

The code is as follows:


StdClass Object
(
[Item1] => stdClass Object
(
[Item11] => stdClass Object
(
[N] => chenling
[M] => llll
)
[Sex] => male
[Age] => 25
)
[Item2] => stdClass Object
(
[Item21] => ling
[Sex] => Female
[Age] => 24
)
)


For example, if I want to obtain the property whose value is chenling, we should access it like this:
$ J-> item1-> item11-> n; // This will get the value of attribute n: chenling
In fact, this access form is similar to accessing common object attributes, and is also equivalent to accessing a 3-dimensional array.

The code is as follows:


// Form 2: mixed object and array
$ Json = '{"item1": [{"name": [{"chen ":
"Chenling", "ling": "chenli"}], "sex ":
"Male", "age": "25" },{ "name": "sun", "sex ":
"Female", "age": "24"}]} ';
$ J = json_decode ($ json );
Print_r ($ J );
Output:
StdClass Object
(
[Item1] => Array
(
[0] => stdClass Object
(
[Name] => Array
(
[0] => stdClass Object
(
[Chen] => chenling
[Ling] => chenli
)
)
[Sex] => male
[Age] => 25
)
[1] => stdClass Object
(
[Name] => sun
[Sex] => Female
[Age] => 24
)
)
)


For example, if you want to obtain the element whose value is chenling, you should access it as follows:
$ J-> item1 [0]-> name [0]-> chen; // This will get the value of the element chen: chenling
In fact, the JSON format of this PHP application combines the access methods of objects and arrays, which is also equivalent to accessing a 5-dimensional array.

The code is as follows:


// Form 3: Full array form
$ Json = '[["item1", "item11"], [
"N", "chenling"], ["m", "llll"] ';
$ J = json_decode ($ json );
Print_r ($ J );
Output:
Array
(
[0] => Array
(
[0] => item1
[1] => item11
)
[1] => Array
(
[0] => n
[1] => chenling
)
[2] => Array
(
[0] => m
[1] => llll
)
)


For example, if you want to obtain the element whose value is chenling, you should access it as follows:

$ J [0] [1]; // the element whose element value is chenling.

However, there is a disadvantage in this method: strings cannot be used as indexes and only numbers can be used. this problem can be solved in the form of full objects. In fact, this access form is an array access method, it is equivalent to accessing a 2-dimensional array.

JSON summary of PHP applications:

From the JSON example of the PHP application, we can see that JSON is a bit similar to XML. It can also transfer structured data between PHP and Javascript, which is very convenient to use.
Note that each attribute and attribute value are enclosed by quotation marks.

Http://www.bkjia.com/PHPjc/326491.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/326491.htmlTechArticlephp json_decode return data js processing php json_decode, return to the front-end data such as: encode_str = "{" green ": 10," size ": 5," strock ": 12} then js uses eval ("obj =" + encode_str +...

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.