In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the PHP application of JSON skills, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The js processing of the data returned by php json_decode
After php json_decode, the data returned to the front desk, such as: encode_str = "{" green ": 10," size ": 5," strock ": 12}
Then js passes through eval ("obj =" + encode_str + ";")
The json data can be instantiated as an object, and the data can be obtained by obj.green directly.
In Javascript, you can use {} to represent an object and [] to represent an array, such as:
Var obj= {"a": "v", "b": "x"}; / / this means that the variable obj is an object, which has two properties: an and b, and the attribute values are: v and x.
Var arr= ["v", "x"]; / / this means that the variable arr is an array with two elements with indexes of 0 and 1, and values of v and x.
JSON is actually a format in which these two formats are mixed together to represent the logical structure of data. In fact, JSON is a mixture of objects and arrays in Javascript.
PHP provides special functions to generate and parse data in JSON format. The data parsed by PHP has the same meaning as the original Javascript data, that is, Javascript objects are parsed into PHP objects, Javascript arrays are parsed into PHP arrays, and PHP uses JSON functions: json_encode ($PHPcode)
The function used by PHP to parse JSON is: json_decode ($JSONcode)
Therefore, there are many forms of JSON, and different forms are different after the interpretation of PHP.
The copy code is as follows:
/ / form 1: it is entirely in the form of objects, and this form of data is available in Javascript
It is also called a correlation array, which is different from a general array.
It can be accessed by using a string as an index (using "[]" or "."
To represent the hierarchy)
$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 copy 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 get the property whose value is chenling, I should access it like this:
$J-> item1- > item11- > ninterbank / this will get the value of attribute n: chenling
In fact, this form of access is similar to accessing ordinary object properties, and it is also equivalent to accessing a 3D array.
The copy code is as follows:
/ / form 2: mixing objects and arrays
$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 I want to get the element whose value is chenling, I should access it like this:
$J-> item1 [0]-> name [0]-> chen;// this will get the value of the element chen: chenling
In fact, this form of PHP application JSON combines the access of objects and arrays, which is also equivalent to accessing a 5-dimensional array.
The copy 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 I want to get the element whose value is chenling, I should access it like this:
$J [0] [1]; / / the element that will get the element value chenling
But there is a disadvantage in this way, that is, you can't use a string as an index, you can only use numbers, and you can solve this problem in the form of a complete object. In fact, this form of access is the access way of an array, which is equivalent to accessing a two-dimensional array.
Summary of PHP application JSON:
From the above PHP application JSON example, we can see that JSON is a bit similar to XML, and it can also transfer structured data between PHP and Javascript, which is very convenient to use.
It should be noted that each attribute and attribute value is enclosed in quotation marks.
Thank you for reading this article carefully. I hope the article "what are the JSON skills of PHP applications" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.