Json definition, standard format, and json string validation _ json

Source: Internet
Author: User
Today we will share and summarize some basic json definitions, formats, and string formats, as well as some simple validation when using json during testing, more and more projects and development plug-ins now support and use the json data format by default, as a way of data persistence and transmission.
There are many other formats. For example, a maximum of xml is used. The standard data format of webservice.
However, because json is a JavaScript Object Notation (JavaScript Object Notation), it naturally supports js well,
So now many ajax and so on use json, but it will be better processed.

1. Definition

What is JSON?
1. JSON refers to the JavaScript Object Notation (JavaScript Object Notation)
2. JSON is a lightweight text data exchange format.
3. JSON is independent of the Language *
4. JSON is self-descriptive and easier to understand
* JSON uses JavaScript syntax to describe data objects, but JSON is still independent of the language and platform. The JSON parser and JSON library support many different programming languages.
W3c standards are described as follows:
JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy to read and write. It is also easy to parse and generate machines. It is based on a subset of JavaScript Programming Language, 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.

JSON is constructed in 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 ).
These are common data structures. In fact, most modern computer languages support them in some form. This makes it possible to exchange a data format between programming languages that are also based on these structures.

JSON has the following forms:

An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.

Note: If the background directly transmits the object to js, it is in json format. References:

[Instructions on how to transmit Jquery + asp.net background data to front-end js for parsing]
[Implementation of passing objects (arrays) to the background and parsing using jquery ajax]

An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.




Note: It is easy to understand how to transmit data. It is actually an array. For the direct relationship between array and json, see the article:

[Some instructions on javascript objects and array json]

2. json standard:

Json: I have no standard. Haha. The standard I am talking about refers to what kind of formatted string js can be parsed into json?

The json string below is correct:

The Code is as follows:


{
"Employees ":[
{
"FirstName": "Bill ",
"LastName": "Gates"
},
{
"FirstName": "George ",
"LastName": "Bush"
},
{
"FirstName": "Thomas ",
"LastName": "Carter"
}
]
}

Note:

Json string, which is parsed into an object in js. The object name is as follows:
Employees and firstName must be caused by "" or. If the value following is int or bool (js is not differentiated), you do not need to use "" or ''. Other strings must be caused, in particular, remember to include the time and date!

Summary:

An object is in it. If multiple objects are in it, they are separated by commas (,), that is, {}, {}, which forms an object sequence. In order to identify the start and end, if you need to add [] for the transfer, the actual transfer form should be [{}, {}]. If you only need to pass an object, then the form of {} can be used. The next step is how the object property is displayed. The property must be in ""
In, the attribute and value are separated by:, and the attribute are separated by,. If the attribute value is an array, it is included in []. In this way, the actual transmitted data format may include: 1. {"attribute 1": value 1, "attribute 2": Value 2}. If the value is a string, enclose it with "" (the same below ). 2. {"property 1": value 1, "Property 2": [value 1, value 2]}, where property 2 is an array containing
Value 1 and value 2. 3. {"property 1": value 1, "Property 2": {"property a": Value a, "Property B": [value B, value c]}, this is complicated. Attribute 2 is an object. This object consists of attributes a and B, and attribute B is an array containing values B and c. I think these should be the most basic, and the rest is the expansion on this basis.

In reality, to serialize an object, you only need to call Newtonsoft. Json. JavaConvert. SerializeObject (); function in. net.

Refer:

Jquery ajax transfers objects (arrays) to the background and parses the implementation

Http://www.jb51.net/article/40131.htm

3. There are so many requirements for json (in fact, there are not many). How can we judge its correctness?

There is a way to print out the string and match it according to the above structure, or directly use an online inspection tool for testing.

Reference: http://www.kjson.com/
Http://tool.oschina.net/codeformat/json

Related Article

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.