Json definition standard format and json string validation

  • 2020-03-30 02:55:22
  • OfStack

JSON is constructed in two structures:

JSON has the following forms:

An object is an unordered collection of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with"} "(right parenthesis). Each name is followed by: (colon); "Name/value" pairs are separated by ", "(comma).

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201405/object.gif" >

Note: if the object is passed to js directly from the background, it is in json format. Reference:

"(link: #)"
"(link: #)"

An array is an ordered collection of values. An array starts with "[" (left bracket) and ends with"] "(right bracket). Values are separated by ", "(comma).

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201405/array.gif" >


Note: passing data is easy to understand. It's just an array. For the direct relationship between array and json, please refer to the article:

[some explanatory notes on javascript objects and array json]

2. Json standard:

Json says: I have no standard. Ha ha. By standard, I mean what kind of formatted string js can be parsed into json?

The following json string is correct:


{
    "employees": [
        {
            "firstName": "Bill",
            "lastName": "Gates"
        },
        {
            "firstName": "George",
            "lastName": "Bush"
        },
        {
            "firstName": "Thomas",
            "lastName": "Carter"
        }
    ]
}

Note:

Json strings, which are parsed into objects in js. The name of the object is:
Employees, firstName, you have to cause it with "" or ". If the value is an int or a bool, you don't need to use "" or ". Other strings must be used, especially the time and date!

The summary is as follows:

Inside is an object, if it is more than one object, with a comma interval, that is,{},{}, so that the composition of an object sequence, in order to distinguish the beginning and end, you need to add [] in order to pass, then the actual form of the transfer should be [{},{}], if only pass an object, then the form of {}. And then there's the representation of the properties of the object, the properties have to be in ""
If the value of the attribute is an array, then include it with []. In this way, the data format actually passed may be: 1, {" attribute 1": value 1," attribute 2": value 2}, if the value is a string, also need to be enclosed with "" (the same below). 2. {" attribute 1": value 1," attribute 2":[value 1, value 2]}, where attribute 2 is an array containing
The values 1 and 2. {" attribute 1": value 1," attribute 2":{" attribute a": value a," attribute b":[value b, value c]}}, this is more complicated, attribute 2 is an object, this object by containing attribute a and attribute b, attribute b is an array containing value b and value c. I think these should be the most basic, the rest is just an extension on this basis.

Practice, to be serialized objects in.net Newtonsoft. Just call in Json. JavaConvert. SerializeObject (); functions

Reference:

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

(link: #)

3. Json has so many requirements (not much), how can we judge its correctness?

One way to do this is to print out the string and use your own to correspond to the above structure, or simply use the online validator.

Reference: (link: http://www.kjson.com/)
(link: http://tool.oschina.net/codeformat/json)


Related articles: