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

What is the lightweight data interchange format JSON

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how the lightweight data interchange format JSON is, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

JSON definition

JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy to read and write, as well as easy to parse and generate by machines. It is based on a subset of the JavaScript programming language in the ECMA262 language specification (third edition 1999-12). JSON uses a text format that is independent of the programming language, but it also uses a C-like language (including the habits of the C-like language (including the habits of the C-like language, such as Java, Java, Perl, Python, etc.), these features make JSON an ideal data exchange format.

The structure of JSON is based on the following two points

1. In different languages, the collection of name / value pairs is understood as objects (object), records (record), structures (struct), dictionaries (dictionary), hash tables (hash table), key lists (keyed list), etc.

two。 An ordered list of values is understood as an array in most languages

JSON uses:

JSON represents a JavaScript object in a specific string form. If you assign a string of such a form to any JavaScript variable, the variable becomes an object reference, and the object is built out of the string, which seems to be a bit of a mouthful, and we'll use an example to illustrate it.

Let's assume that we need to create a User object with the following properties

User ID

User name

User Email

You can use the following JSON form to represent User objects:

{"UserID": 11, "Name": "Truly", "Email": "zhuleipro@hotmail.com"}

Then if you assign this string to a JavaScript variable, you can use any of the properties of the object directly.

Complete code:

< script > var User = {"UserID": 11, "Name": "Truly", "Email": "zhuleipro@hotmail.com"}; alert (User.Name); < / script >

It may be a little more complicated in actual use, for example, we define a more detailed structure for Name so that it has FirstName and LastName:

{"UserID": 11, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "zhuleipro@hotmail.com"}

Complete code:

< script > var User = {"UserID": 11, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "zhuleipro@hotmail.com"}; alert (User.Name.FirstName); < / script >

Now that we add a new requirement, we need a list of users for a page, not just a single user information, so we need to create an array of user lists.

The following code demonstrates defining this user list in JSON form:

[{"UserID": 11, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "zhuleipro@hotmail.com"}, {"UserID": 12, "Name": {"FirstName": "Jeffrey", "LastName": "Richter"}, "Email": "xxx@xxx.com"}, {"UserID": 13, "Name": {"FirstName": "Scott", "LastName": "Gu"} "Email": "xxx2@xxx2.com"}]

Complete code:

< script > var UserList = [{"UserID": 11, "Name": {"FirstName": "Truly", "LastName": "Zhu"}, "Email": "zhuleipro@hotmail.com"}, {"UserID": 12, "Name": {"FirstName": "Jeffrey", "LastName": "Richter"}, "Email": "xxx@xxx.com"}, {"UserID": 13, "Name": {"FirstName": "Scott", "LastName": "Gu"} "Email": "xxx2@xxx2.com"}] Alert (UserList [0] .Name.FirstName); < / script >

In fact, except for the use of "." In addition to referencing properties, we can also use the following statement:

Alert (UserList [0] ["Name"] ["FirstName"]); or alert (UserList [0] .Name ["FirstName"])

Readers should now know something about the use of JSON, which can be summarized as follows:

An object is a collection of properties and value pairs. An object begins with "{" and ends with "}". Each property name and value are prompted by ":", and the attributes are separated by ",".

An array is a collection of sequential values. An array begins with "[" and ends with "]", with values separated by ",".

Values can be strings, numbers, true, false, null, objects or arrays in quotation marks. These structures can be nested.

The definitions of strings and numbers are basically the same as those of C or Java.

About how the lightweight data interchange format JSON is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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