Use of indispensable JSON in Web Applications

Source: Internet
Author: User
The form returned by the server when we serve the server. There are usually three types of products: HTML, JSON, and jsonp. As I participated in the Qilu software competition this year, I learned a little bit about it and I want to share it with you. I will share with you over the past few days what JSON is, how to use JSON, and how to use JSON when requesting a service.

 

Let's talk about the basic knowledge of JSON today. Please refer to the following

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy for reading and writing, and easy for machine parsing and generation.

Directory

Infrastructure

Basic example

1.
Name/value pair

2.
Array

Format Application

1.
Assign JSON data to a variable

2.
Access Data

3.
Modify JSON data

4.
Convert back to string

Specific form

Concept comparison

JSON online verification tool

1.
Preface

2.
Features

Expand

Infrastructure

Basic example

1.
Name/value pair

2.
Array

Format Application

1.
Assign JSON data to a variable

2.
Access Data

3.
Modify JSON data

4.
Convert back to string

Specific form

Concept comparison

JSON online verification tool

1.
Preface

2.
Features

Expand

Edit this sectionInfrastructure

JSON construction has two structures:

1. A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ).

2. An ordered list of values ). In most languages, it is understood as an array ).

Edit this sectionBasic example

In short, [1], JSON can convert a set of data represented in Javascript objects into strings, and then it can be easily passed between functions, alternatively, the string is transmitted from the Web Client to the server in an asynchronous application. This string looks a bit odd, but javascript can easily explain it, and
JSON can represent a more complex structure than "name/value pairs. For example, it can represent arrays and complex objects, not just a simple list of keys and values.

Name/value pair

In the simplest form, you can use the following JSON to represent "name/value pairs ":

{"Firstname": "Brett "}

This example is very basic and actually takes more space than the equivalent plain text "name/value pair:

Firstname = Brett

However, when you concatenate multiple "name/value pairs", JSON will reflect its value. First, you can create records containing multiple "name/value pairs", such:

{"Firstname": "Brett", "lastname": "McLaughlin", "email": "aaaa "}

In terms of syntax, this is not a great advantage over "name/value pairs", but JSON is easier to use and more readable in this case. For example, it explicitly indicates that the above three values are part of the same record; curly braces make these values have a certain relationship.

Array

To represent a group of values, JSON not only improves readability, but also reduces complexity. For example, assume that you want to list a person's name. In XML, many start and end tags are required. If a typical name/value pair is used (like the name/value pair seen in the previous articles in this series ), you must either create a proprietary data format or change the key name to a person1-firstName.

If JSON is used, you only need to group multiple records with curly braces:

{"People ":[

{"Firstname": "Brett", "lastname": "McLaughlin", "email": "aaaa "},

{"Firstname": "Jason", "lastname": "Hunter", "email": "BBBB "},

{"Firstname": "elliotte", "lastname": "Harold", "email": "CCCC "}

]}

This is not hard to understand. In this example, there is only one variable named people. The value is an array containing three entries, each of which is a one-person record, including the name, last name, and email address. The preceding example demonstrates how to use parentheses to combine records into a value. Of course, you can use the same syntax to represent multiple values (each value contains multiple records ):

{"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": "Science Fiction "},

{"Firstname": "tad", "lastname": "Williams", "genre": "Fantasy "},

{"Firstname": "Frank", "lastname": "Peretti", "genre": "Christian fiction "}

],

"Musicians ":[

{"Firstname": "Eric", "lastname": "Clapton", "instrument": "Guitar "},

{"Firstname": "Sergei", "lastname": "Rachmaninoff", "instrument": "Piano "}

]}

The most noteworthy here is that it can represent multiple values, and each value contains multiple values. However, you should also note that the actual name/value pairs in the record can be different between different primary entries (programmers, authors, and musicians. JSON is completely dynamic and allows you to change the data representation mode in the middle of the JSON structure.

When processing data in JSON format, there are no predefined constraints to be observed. Therefore, in the same data structure, you can change the way data is represented, or even express the same thing in different ways.

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.