What are the format types of JSON?

Time:2021-6-8

JSON is the most popular data exchange format at present. For it, we need to be very familiar with all aspects of its knowledge. This time, let’s take a look at its format types.

Data exchange format

  • Basic types
  • Array type
  • Object nesting

Case practice

Basic types

{“key”: value, “key”: “value”,…}, start with braces, add colon to the name of the key, and then follow the corresponding value. If there are other key value pairs, separate them with commas.

{
 "Name": Zhang San,
 "age": 18,
 "sex": true
}

Array type

[{key: value, key: value}, {key: value, key: value},}], start with brackets, and the data between them is separated by commas.

[
 {
 "Name": Zhang San,
 "age": 18,
 "sex": true
 },
 {
 "Name": "Li Si",
 "age": 19,
 "sex": false
 }
]

Object nesting

From the above two types, because the value is not fixed, a variety of nested types can be evolved.

{
 "name": "teacher",
 "computer": {
 "CPU":"intel7,
 "disk": "512G"
 },
 "students": [
 {
 "Name": Zhang San,
 "age": 18,
 "sex": true
 },
 {
 "Name": "Li Si",
 "age": 19,
 "sex": false
 }
 ]
}

More details and formats to pay attention to can also be found directly in theJSONSee the official website.

Extend ~ XML

XMLIt is also a data exchange format. It does not refer to a single language, but a cross language data format. At present, JSON is gradually replacing it, but we can still see it frequently in various places. Therefore, understanding it is also a compulsory course for us. Let’s also take a look at its syntax format rules.

All XML elements must have a close tag

In HTML, you often see elements without closing tags:

<p>This is a paragraph
<p>This is another paragraph

In XML, it is illegal to omit the close tag. All elements are_ Must_ For closed label:

<p>This is a paragraph</p>
<p>This is another paragraph</p> 

notes:You may have noticed that the XML declaration does not close the tag. It’s not a mistake. Declarations are not part of XML itself. It’s not an XML element, and it doesn’t need to close the tag.

XML tags are case sensitive

XML elements are defined using XML tags.

XML tags are case sensitive. In XML, the label < letter > is different from the label < letter >.

You must write open and close tags with the same case:

< message > this is wrong</ message>
​
< message > this is correct</ message>

notes:Open and close tags are often referred to as start and end tags. No matter which term you like, the concept is the same.

XML must be nested correctly

In HTML, you often see elements that are not nested correctly:

<b><i>This text is bold and italic</b></i>

In XML, all elements are_ Must_ Nest each other correctly:

<b><i>This text is bold and italic</i></b>

In the example above, correct nesting means: because the element is inElement, then it must be in theElement.

An XML document must have a root element

An XML document must have one element that is the source of all other elements_ The parent element. This element is called_ The root element.

<root>
 <child>
 <subchild>.....</subchild>
 </child>
</root>

XML attribute values must be quoted

Like HTML, XML can have attributes (name / value pairs).

In XML, XML attribute values must be quoted. Study the following two XML documents. The first is wrong and the second is right

<note date=08/08/2008>
 <to>George</to>
 <from>John</from>
</note> 
<note date="08/08/2008">
 <to>George</to>
 <from>John</from>
</note> 

The error in the first document is that the date attribute in the note element is not quoted.