Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and grammatical rules of JSON

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "introduction and grammatical rules of JSON". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the introduction and grammar rules of JSON.

1. What is json?

JSON, whose full name is JavaScript Object Notation, is a lightweight data exchange format. JSON has the same characteristics as XML, which is a data storage format, but JSON is easier for people to write and read, easier to generate and parse than XML.

Syntax rules for 2.JSON:

1) the data is saved as a name / value pair, which includes the field name (in double quotes), followed by a colon, followed by a value, such as "firstName": "John". The value of JSON can be a number, string, null, true, false, array, or object.

2) data can be easily separated.

3) curly braces save objects, which can contain multiple name / value pairs.

4) Square brackets hold an array, which can contain multiple objects.

5) JSON file:

The file type of the JSON file is ".json"

The MIME type of JSON text is "application/json"

There are two structures of 3.JSON:

1) the form of key-value pairs, that is, the structural set of Name-Value pairs (unordered). For example: {name1:value1.name2:value2,...}

{"name": "Xiaoming", "age": "12"}

2) Array: an ordered list of data. For example: [value1,value2,value3,.], where values can be double-quoted strings (string), numeric values (number), true, false, null, objects (object), or arrays (Array), all of which can be nested. Such as:

Example 1: an object can contain an array

{"root": [{"id": "001", "name": "Xiao Hong"}, {"id": "002", "name": "Xiao Ming"}, {"id": "003", "name": "Xiao Li"}], "total": 3, "success": true}

Example 2: you can also nest sub-objects by objects, and then nest arrays of sub-objects:

{"memberList": {"lists": [{"id": "001", "name": "Xiao Hong"}, {"id": "002", "name": "Xiao Ming"}, {"id": "003", "name": "Xiao Li"}]}

Example 3: an array can contain objects:

[{"id": "001", "name": "Xiao Hong"}, {"id": "002", "name": "Xiao Ming"}, {"id": "003", "name": "Xiao Li"},.]

The value and modification methods of 4.JSON:

Such as:

Var employees = [

{"firstName": "Bill", "lastName": "Gates"}

{"firstName": "George", "lastName": "Bush"}

{"firstName": "Thomas", "lastName": "Carter"}

]

Value: employees [0] .lastName

Modify data: employees [0] .lastName = "Jobs"

Conversion method between 5.JSON and string:

Convert JSON to string (string) format: var string=JSON.stringify (obj)

Convert Json strings to JSON:

(1) var data=JSON.parse (string)

Function strToJson () {

Return JSON.parse (str)

}

When using JSON.parse, you must strictly abide by the JSON specification. For example, all attributes need to be enclosed in quotation marks, otherwise errors will occur, as follows:

Var str ='{name: "jack"}'

Var obj = JSON.parse (str); / /-- > parse error

Name is not enclosed in quotation marks. Using JSON.parse, all browsers throw exceptions and parsing fails. (there are no problems in the following two ways)

(2) eval (string)

For example: function myEval () {

Var str=' {"id": "001", "name": "Xiaoming", "age": 10}'

Var obj=eval ('('+ str+')')

Alert (obj.toJSONString ())

}

(3) new Function form

Function strToJson (str) {

Var json= (new Function ("return" + str)) ()

}

(4) there is also a method jQuery.parseJSON (json) in jQuery to convert a string to JSON format, which accepts a JSON string in a standard format and returns the parsed JavaScript (JSON) object.

At this point, I believe you have a deeper understanding of the introduction and grammatical rules of JSON. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report