Json format and analysis (1)

Json format explanation



foreword

This article explains the format of Json and the format of common combination methods, which is more suitable for beginners to read
Json format conversion URL

Reprint please indicate the source! ! ! ! ! !


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is JSON?

To put it bluntly speaking, Json is a lightweight text data exchange format. If you are a novice, you must not underestimate its use. Compared with XML, it is more readable. Among them, its data The processing function is also very powerful.

2. The main symbols and grammar rules of Json

1. Import library

The data is in the name/value pair: key: value
data is separated by commas:, (must be in English) braces
{} save the object (braces ({}) are equivalent to the existence of a class in various languages)
Brackets [] save the array, and the array can contain multiple objects (brackets ([ ]) represent an array in the language, and the types in the array can be defined by yourself)

2. Json name/value pairs

The writing format of JSON data is:

key : value

Name/value pairs consist of the field name (in double quotes), followed by a colon, then the value:

"name": "Sakura Pink's Small Shovel"

3. JSON value

JSON values ​​can be:
numbers (integer or floating point)
strings (in double quotes)
logical values ​​(true or false)
arrays (in square brackets)
objects (in curly brackets)
null

4. JSON numbers

JSON numbers can be integers or floating point types:
{ "age":30 }
If you are a number, you don't need to use " " double quotes, and if you put double quotes, it will be represented as a string

5. JSON object

JSON objects are written within braces {}:

{key1 : value1, key2 : value2, … KeyN : valueN }

I explained to you above that the { } in Json is equivalent to a class.
insert image description here
I wrote a curly brace { } on the outermost side. This curly brace is equivalent to the Root class in C#, and the " item" means that the name is defined for the class. The structure is equivalent to the Class Item displayed next to it. At this time, the item in the outermost curly braces is equivalent to defining an Item type attribute in the Root class.

6. JSON array

[
{ key1 : value1-1 , key2:value1-2 },
{ key1 : value2-1 , key2:value2-2 },
{ key1 : value3-1 , key2:value3-2 },

{ key1 : valueN-1 , key2:valueN-2 },
]
Use the online conversion format tool to convert the format as shown below
insert image description here
{ "item":[ { "name": "Little Shovel", "age":18}, { "name": "Little A giao ", "age":88}


]

}
The json format is converted into C# as follows

public class ItemItem
{
    
    
    /// <summary>
    /// 小铁锹
    /// </summary>
    public string name {
    
     get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int age {
    
     get; set; }
}
 
public class Root
{
    
    
    /// <summary>
    /// 
    /// </summary>
    public List <ItemItem > item {
    
     get; set; }
}

The outermost curly braces { } are still equivalent to Class Root, and the square brackets [ ] we are talking about here are equivalent to a class. Here, the converted data for us has become a generic collection item. And his variable is our class ItemItem, and our ItemItem class has our name attribute, our age attribute and the value followed by our name is a string type, use "" double quotes in Json to give Quoting, but the value behind our age does not add double quotes "", everyone still needs to combine the Json format into our language to understand, so that everyone can understand more clearly how our data is stored and expressed in json of.

The parameter types in the array must be consistent, otherwise the Json format will be wrong.

The array in Json is the same as the array in our language. As long as the type in the array is determined, it must follow this format. My following way of writing is wrong way of writing.
insert image description here
The first value of my item array is an object { }, which is a class, and my second value is a string, and an error will be reported at this time. At this time, just delete the string, and if we pass another object, it will not report an error, but the format in the object must be consistent with the format of our first object, otherwise an error will also be reported. Look at the following display.
insert image description here
At this time, the object below is missing an array compared to the object above. This format is obviously wrong, and an error will be reported at this time. Just add the array.

Array nesting is also supported in Json

insert image description here
I have nested three layers of arrays here. You can take a closer look. The outermost array of the nested array must have an array name, but the inner array cannot add an array name.

7. JSON Boolean

When it comes to Boolean values, it is nothing more than true or false. How is Boolean value passed in Json? You can think about whether to add double quotes or the same way as the value?
answer revealed

{ “flag”:true }

Boolean values ​​in Json are assigned in the same way as numeric values

8.JSON null

JSON can set null values:

{ "runoob":null }
The use of null is consistent with the use of boolean values ​​and numeric values.

Summarize

Today I mainly introduce the grammar format of Json to you. Although there are only a few simple formats, they can be used in a variety of ways, so everyone must lay a good foundation for themselves, so as to better pave the way for the future.

Guess you like

Origin blog.csdn.net/m0_45244541/article/details/123925932