Learn the JSON data structure of JSON from zero

  • 2020-03-30 02:59:08
  • OfStack

Recently the WeChat platform has been developed to use JSON for data exchange. JSON has been used before, but only for...

In the development of WeChat platform, the JSON form is as follows:
Code snippet 1:


{
     "button":[
     {    
          "type":"click",
          "name":" Today's song, ",
          "key":"V1001_TODAY_MUSIC"
      },
      {
           "type":"click",
           "name":" Introduction of singer ",
           "key":"V1001_TODAY_SINGER"
      },
      {
           "name":" The menu ",
           "sub_button":[
           {    
               "type":"view",
               "name":" search ",
               "url":"http://www.soso.com/"
            },
            {
               "type":"view",
               "name":" video ",
               "url":"http://v.qq.com/"
            },
            {
               "type":"click",
               "name":" Give us a thumbs up ",
               "key":"V1001_GOOD"
            }]
       }]
 }

We then use PHP's json_encode() function to convert a two-dimensional array into JSON

  But the converted JSON form:
  Code snippet 2:


{
    "button": {
        "1": {
            "type": "click",
            "name": " Today's song, ",
            "key": "V1001_TODAY_MUSIC"
        },
        "2": {
            "type": "click",
            "name": " Introduction of singer ",
            "key": "V1001_TODAY_SINGER"
        },
        "3": {
            "name": " The menu ",
            "sub_button": [
                {
                    "type": "view",
                    "name": " search ",
                    "url": "http://www.soso.com/"
                },
                {
                    "type": "view",
                    "name": " video ",
                    "url": "http://v.qq.com/"
                },
                {
                    "type": "click",
                    "name": " Give us a thumbs up ",
                    "key": "V1001_GOOD"
                }
            ]
        }
    }
}

You can see that the form is inconsistent.

You can only see the structure of JSON.
JSON has two types of data: 1. Unordered object structure; 2. Ordered array structure
1. Unordered object structure
Unordered object structures are called differently in different languages, such as dictionaries in Python and JSON objects in JS...
Anyway, it's a combination of key/value pairs.
The JSON structure I just converted is an unordered combination of key/value pairs
2. Ordered array structure
An ordered array structure, as shown in code snippet 2.
By converting JSON from an array to an ordered array, you get an ordered JOSN array structure.


Related articles: