The use of JSON data type in MySQL (1)-JSON introduction
JSON refers to JavaScript Object Notation, that is, JavaScript Object Notation. JSON is a lightweight text data exchange format that is self-descriptive and easier to understand. JSON uses Javascript syntax to describe data objects, but JSON is still independent of language and platform. The JSON parser and JSON library support many different programming languages.
1. JSON syntax
1. JSON syntax rules
JSON syntax is a subset of JavaScript object representation syntax:
(1) Data is in name/value pairs;
(2) Data is separated by commas;
(3) Curly braces hold objects;
(4) Square brackets hold arrays.
2. JSON name/value pairs
The writing format of JSON data is: name/value pairs.
The name/value pair includes the field name (the field name is enclosed in double quotes), followed by a colon, and then the value. such as:
"name" : "Tom"
3. JSON value
JSON values can be: numbers, strings (enclosed in double quotation marks), logical values (true or false), arrays (enclosed in square brackets), objects (enclosed in braces), null.
(1) JSON number
JSON numbers can be integer or floating point, for example:
{ "age":18 }
(2) JSON object
JSON objects are placed in curly braces. A JSON object can contain multiple name/value pairs, such as:
{ "name":"Tom" , "age":18 }
(3) JSON array
JSON arrays are placed in square brackets. An array can contain multiple objects, such as:
{ "sno":"20190224001",
"name":"ZhangPeng"
"contact":[{ "phone":"13703732215" , "addr":"河南省新乡市向阳小区" },
{ "phone":"0373-2215666" , "addr":"河南省新乡市国际旅行社" }
]
}
(4) Logical value
The logical value of JSON is represented by true or false, for example:
{ "flag":true }
(5)null
JSON can set a null value, such as:
{ "note":null }
Second, the JSON object
The JSON object is placed in curly brackets, and the object can contain multiple key/value (key/value) pairs. The key must be a string, and the value can be a valid JSON data type (string, number, object, array, logical value, or null).
Use a colon (:) to separate key and value; use a comma (,) to separate each key/value pair.
For example:
{ "name":"Tom", "age":18, "PartyMember":false, "note":null }
A JSON object can contain another JSON object, that is, JSON objects can be nested. such as:
{ "name":"ZhangTao",
"age":25,
"contact": {
"phone":"15802587788",
"QQ":"32578905",
"wechat":"15802587788"
}
}
Three, JSON array
1. Array as JSON object
The JSON array is placed in square brackets. The array value in JSON must be a valid JSON data type (string, number, object, array, logical value or null). E.g:
{
"Tom" , "John" , "Rose"}
2. Array in JSON object
The value of an object property can be an array, such as:
{ "id":"20190224001",
"name":"ZhangTao",
"hobbies":[ "basketball", "football", "singing" ]
}