JSON (JavaScript Object Notation)is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. It is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999.
JSON is a data format that Douglas Crockford began to promote in 2001. It officially became a mainstream data format from 2005 to 2006. It was at that time that Yahoo and Google began to use the JSON format extensively.
JSON syntax rules
In the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, etc. However, objects and arrays are two special and commonly used types:
Objects are represented as key-value pairs
Data is separated by commas
Curly braces save objects
Square brackets save arrays
JSON key/value pairs
JSON key-value pairs are a way to save JS objects, and they are also written in the same way as JS objects. Much the same, the key name in the key/value pair combination is written in front and wrapped in double quotes "", separated by colon:, and then followed by the value:
{"firstName": "Json"}
This is easy to understand, equivalent to this JavaScript statement:
{firstName : "Json"}
Two structures of JSON
JSON has two representation structures, objects and arrays.
The object structure starts with "{" braces and ends with "}" braces. The middle part consists of 0 or more "key (keyword)/value (value)" pairs separated by ",". Keywords and values are separated by ":", and the syntax structure is like code.
{ key1:value1, key2:value2, ... }
The keyword is a string, and the value can be a string, a numerical value, true, false, null, an object or an array
The array structure starts with "[" and ends with "]" . The middle consists of 0 or more value lists separated by ",", and the syntax structure is like code.
[ { key1:value1, key2:value2 }, { key3:value3, key4:value4 } ]
The above is the detailed content of How to use json. For more information, please follow other related articles on the PHP Chinese website!