Hello Qt -- Introduction to Jason

Posted by Ryoku on Sat, 05 Mar 2022 17:18:50 +0100

1, Introduction to JSON

1. Introduction to JSON

JSON(JavaScript Object Notation) is a lightweight data exchange format based on a subset of JavaScript (Standard ECMA-262 3rd Edition - December 1999). JSON adopts a text format that is completely independent of the language and uses the habit of C-like language family (including C, C + +, c#, Java, JavaScript, Perl, Python, etc.).

JSON uses JavaScript syntax to describe data objects, but JSON is still language and platform independent. The JSON parser and JSON library support many different programming languages.

2. JSON syntax

JSON syntax is a subset of the French representation of JavaScript objects. The syntax rules are as follows:

A. Data in name / value pair

B. Data is separated by commas

C. Save object curly braces

D. Square brackets hold the array

3. JSON name / value pair

The writing format of JSON data is: name / value pair.

The name / value pair includes the field name (in double quotes), followed by a colon, followed by the value:

"firstName" : "John"

4. JSON value

JSON value type:

Number (integer or floating point number)

String (in double quotes)

Logical value (true or false)

Array (in square brackets)

Object (in curly braces)

null

5. JSON object

JSON objects are written in curly braces:

An object can contain multiple name / value pairs:

{ "firstName":"John" , "lastName":"Doe" }

6. JSON array

JSON arrays are written in square brackets:

The array can contain multiple objects:

{

"employees": [

{ "firstName":"John" , "lastName":"Doe" },

{ "firstName":"Anna" , "lastName":"Smith" },

{ "firstName":"Peter" , "lastName":"Jones" }

]

}

2, JSON infrastructure

JSON structures have two kinds of structures: objects and arrays.

1. Object

The object represents the contents enclosed by "{}", and the data structure is {key: value,key: value,...} The structure of key value pairs. In object-oriented language, key is the attribute of the object, value is the corresponding attribute value, and the method to obtain the attribute value is: object Key, the type of attribute value can be number, string, array and object.

{"firstName":"Brett"}

{"firstName":"Brett","lastName":"McLaughlin","email":"scorpio@hotmai.com"}

When multiple name / value pairs are strung together, JSON is easier to use and more readable. When multiple name / value pairs explicitly indicate that multiple values are part of the same record, curly braces make a connection between multiple values.

2. Array

The array is represented by the contents enclosed by brackets "[]", and the data structure is ["java","javascript","vb",...], The value is obtained by index. The type of field value can be number, string, array and object.

When you need to represent a set of values, JSON can not only improve readability, but also reduce complexity.

Variables with a single value (including multiple records) are as follows:

{

    "people":[

        {"firstName":"Brett","lastName":"McLaughlin","email":"aaaa"},

        {"firstName":"Jason","lastName":"Hunter","email":"bbbb"},

        {"firstName":"Elliotte","lastName":"Harold","email":"cccc"}

    ]

}

The value of the people variable is an array containing three entries. Each entry is a person's record, including first name, last name and e-mail address.

Variables with multiple values (each value contains multiple records) are as follows:

{

    "programmers": [

        {
        "firstName": "Brett",
        "lastName": "McLaughlin",
        "email": "aaaa"
        }, 

        {
        "firstName": "Jason",
        "lastName": "Hunter",
        "email": "bbbb"
        }, 

        {
        "firstName": "Elliotte",
        "lastName": "Harold",
        "email": "cccc"
        }],

    "authors": [

        {
        "firstName": "Isaac",
        "lastName": "Asimov",
        "genre": "sciencefiction"
        }, 

        {
        "firstName": "Tad",
        "lastName": "Williams",
        "genre": "fantasy"
        }, 

        {
        "firstName": "Frank",
        "lastName": "Peretti",
        "genre": "christianfiction"
        }],

    "musicians": [

        {
        "firstName": "Eric",
        "lastName": "Clapton",
        "instrument": "guitar"
        }, 

        {
        "firstName": "Sergei",
        "lastName": "Rachmaninoff",
        "instrument": "piano"
        }]

}

Topics: JSON Qt