JSON overview

Misunderstanding:

        I have always thought that JSON is an object, with similar characteristics to js objects: {key:value} form,

So that there is a terrible situation in my own thinking: I don't know how to explain what the habitual json is? All kinds of confusion, the result ---> the consequences are serious .

Concept distinction:

1.JSON: (JavaScript Object Notation, js object notation);

2. JSON is a data format, not a programming language;

3. JSON and js have the same syntax format, but JSON does not belong to js;

4. Not only js uses JSON, many programming languages ​​have parsers and serializers for JSON.

JSON key-value pair is a way to save JS objects, and it is similar to the way of writing JS objects. The key name in the key/value pair combination is written in front and wrapped in double quotes "", separated by colon :, and then followed by value.

grammar:

The syntax of JSON can represent the following three types of values:

1. Simple value;

2. Object;

3. Arrays.

Misunderstanding: Consistently think that JSON is just an object or an array, and then measure it by the standard of an array or an object,

But they did not consider the differences between them, and in the end, "one slip into an eternal hatred".

for example:

1. Simple value:

 1 5 // This is how JSON represents the value 5; 2 "Hello world" // The process by which JSON represents a string 

the difference:

1) The biggest difference between js strings and JSON strings: JSON strings must use double quotes (single quotes will cause syntax errors);

2) Boolean and null are also valid JSON forms.

2. Object:

①Object literals in js ( Is it the JSON object you imagined in your heart? Key:value form, error! )

 1 var person = { 2 name:"王五", 3 age:22 4 } 

②Objects in JSON require quotation marks for properties:

So the above object literal can be represented like this:

1 var person = { 
2     "name":"王五",  
3     "age":22};

JSON represents the above objects in the following way:

1 { 
2     "name":"王五",  
3      "age":22
4 }

Compare, is there a difference?

1. There is no concept of variables in JSON, so no variables are declared;

2. No semicolon at the end.

Remember: Object properties must be enclosed in double quotes.

3. Array:

JSON arrays take the form of array literals in JS.

1  var values ​​= [25,"hello", true ]; // Array literals in js
1 [25,"hello", true ]     // array representation in JSON

Sure enough, there are neither variables nor semicolons in the JSON array.


 

Parsing and Serialization

1.JSON.stringify();

2.JSON.parse();

3.toJSON.

Is it a familiar method? Fortunately, I have the opportunity to review it and correct my shortcomings:

        1. There are two methods for JSON objects: stringify() and parse(), which are actually serializing js objects into JSON strings and parsing JSON strings into native js values. By default, the JSON string output by JSON.stringify() does not contain any whitespace strings or indentation;

        2.当JSON.stringify()

When accepting a parameter (the above scenario), JSON is to serialize the js object into a JSON string;

When JSON.stringify() accepts two parameters, and the second parameter is an array, it means that only the properties listed in the array are included in the result; when the second parameter is a function, the situation is slightly different, the function The returned value is the value of the corresponding key. However, when the function returns undefined, the corresponding property will be ignored and deleted;

While it accepts three arguments, the third argument controls indentation and whitespace in the result.

summary:

JSON is a lightweight data format that simplifies the amount of tools to represent complex data structures.

JSON vs js: JSON is a string representation of JS objects, which uses text to represent information about a JS object, which is essentially a string.

In the JS language, everything is an object. Therefore, any supported type can be represented by JSON, such as strings, numbers, objects, arrays, etc.
 
But objects and arrays are two special and commonly used types.
 
Object: Object is the content enclosed by {} in curly braces in JS, and the data structure is a key-value pair structure of {key1: value1, key2: value2, ...}. In object-oriented languages, keys are properties of objects and values ​​are corresponding values. Key names can be represented using integers and strings. The type of the value can be any type.
 
Array: Array is the content enclosed in square brackets [] in JS, and the data structure is the index structure of ["java", "javascript", "vb", ...]. In JS, arrays are a special data type that can also use key-value pairs like objects, but indexes are used more often. Likewise, the type of the value can be any type.

Advanced Programming with JavaScript: pp. 563-570.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324816673&siteId=291194637