Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSON infrastructure


May 08, 2021 JSON


Table of contents


What is JSON

JSON, to put it simply, is a format javaScript uses to process data, which is very easy to use.

JSON, mostly used to process data exchange between JavaScript and the web server, pass data from the background web server to the fore, and then use JavaScript for processing, such as ajax.

JSON supports a large number of languages, including JavaScript, C#, PHP, Java, and so on, due to JSON's independence from the language's lightweight data exchange format, which is somewhat similar to SQL languages.

The two structures of JSON

There are two types of JSON structures:

  1. Object structure;
  2. Array structure;

The structure of the object

The object structure is enclosed with braces that are made up of 0 or more "keyword: value" pairs separated by English commas.

Grammar:

var jsonObj =
{
    "键名1":值1,
    "键名2":值2,
    ……
    "键名n":值n
}

Description:

jsonObj refers to the jason object. T he object structure starts with a " and ends with a """ Where "key name" and "value" are paired with an English colon, and the two "key names: values" are separated by an English comma.

Note that the key name here is a string, but the values can be numeric, string, object, array, or logical true and false.

JSON array structure

The JSON array structure is enclosed in parentheses, which consist of 0 or more lists of values separated by a comma in English.

Grammar:

var arr =
[
    {
        "键名1":值1,
        "键名2":值2
    },
    {
        "键名3":值3,
        "键名4":值4
    },
    ……
]

Description:

arr refers to the jason array. T he array structure ends with the """" I n the JSON array, each pair of "" is equivalent to a JSON object. And the syntax is very similar.

Note that the key name here is a string, but the values can be numeric, string, object, array, or logical true and false.

For the JSON object structure and JSON array structure reading, writing, modifying, deleting and traversing operations, please go to the corresponding section to carefully review.

Code snipper 1

The code is as follows:
{
     "button":[
     {    
          "type":"click",
          "name":"今日歌曲",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "type":"click",
           "name":"歌手简介",
           "key":"V1001_TODAY_SINGER"
      },
      {
           "name":"菜单",
           "sub_button":[
           {    
               "type":"view",
               "name":"搜索",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":"视频",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":"赞一下我们",
               "key":"V1001_GOOD"
            }]
       }]
 }

Then use PHP'json_encode () function to convert a two-dimensional array into JSON
But the converted JSON form:

Code snipper 2

The code is as follows:
{
    "button": {
        "1": {
            "type": "click",
            "name": "今日歌曲",
            "key": "V1001_TODAY_MUSIC"
        },
        "2": {
            "type": "click",
            "name": "歌手简介",
            "key": "V1001_TODAY_SINGER"
        },
        "3": {
            "name": "菜单",
            "sub_button": [
                {
                    "type": "view",
                    "name": "搜索",
                    "url": "http://www.soso.com/"
                },
                {
                    "type": "view",
                    "name": "视频",
                    "url": "http://v.qq.com/"
                },
                {
                    "type": "click",
                    "name": "赞一下我们",
                    "key": "V1001_GOOD"
                }
            ]
        }
    }
}

It can be seen that the form is inconsistent.

You can only look at the structural form of JSON.
JSON has two kinds of data: 1. out-of-order object structure;


1. Out-of-order object structure
Disordered object structures are called differently in different languages, such as the dictionary in Python and the JSON object in JS...
In short, it is a combination of keys/value pairs.
The JSON structure I just converted is a disordered combination of key/value pairs


2. Ordered array structure
An ordered array structure, that is, the structure shown in Code Piece 2.
By converting the array as an ordered array, you can get an ordered JSON array structure.

In the next section, let's get to know the JSON data format!