JSON (JavaScriptObjectNotation) is a lightweight data exchange format, which is called JavaScript Object Notation. One of the advantages of using JSON for data transmission is that JSON is actually JavaScript. It is based on a text format of the subset of the literal Syntax of JavaScript objects in ECMAScript 3rd. What is JSON
JSON: JavaScript Object Notation (JavaScript Object Notation ).
The JSON format is a list of projects surrounded by braces "{}". Each project is separated by a comma (,), and a project is a property name and attribute value separated by a colon. This is a typical dictionary representation, and again shows that the objects in javascript are dictionary structures. No matter how complex an object is, you can use JSON code to create and assign values.
JSON Structure
JSON has two structures:
Json is simply an object and an array in javascript. Therefore, the two structures are objects and arrays. These two structures can represent various complex structures.
1. Object: Content enclosed by the object represented as "{}" in js. The data structure is {key: value, key: value ,...} the structure of the key-value pair. In the object-oriented language, the key is the object attribute, and the value is the corresponding attribute value, so it is easy to understand that the value method is the object. key, which can be a number, String, array, or object.
2. array: The array is enclosed by brackets "[]" in js. The data structure is ["java", "javascript", "vb",...], the value can be obtained using indexes in the same way as in all languages. Field values can be of the following types: Numbers, strings, arrays, and objects.
After the object and array structures, you can combine them into complex data structures.
JSON syntax rules
JSON syntax is a subset of the syntax of JavaScript Object Notation.
Data in name/value pairs
Data is separated by commas (,).
Brackets save objects
Square brackets Save the Array
The JSON value can be:
Number (integer or floating point number)
String (in double quotation marks)
Logical value (true or false)
Array (in square brackets)
Object (in curly brackets)
Null
1) Separate the parallel data with commas.
2) The ing is represented by a colon.
3) The set (array) of the parallel data is represented by square brackets.
4) The ing set (object) is represented by braces.
JSON example
Create an object without any attributes:
The Code is as follows:
Var obj = {};
Create an object and set attributes and initial values:
The Code is as follows:
Var author = {name: "trigkit4", age: 21, sex: "male "};
Create an object and set attributes and methods:
The Code is as follows:
Var hello = {content: "how are you? ", Say: function () {alert (this. content )}};
Create a nested array of other objects and objects:
The Code is as follows:
Var company = {name: "Apple ",
Product: "iPPhone ",
Chairman: {name: "Tim Cook", age: 54 },
Employees: [{name: "Jony Ive", age: 47 },{ name: "Lili", age: 29}],
};
An object is a set of unordered name/value pairs. An object starts with the left branch and ends with the right branch.
A value can be a string enclosed in double quotation marks, a value, a true or false value, an array or an object.
Data Type:
In terms of structure, all data can be divided into three types:
The first type is scalar, which is a separate string or number, such as the word "Beijing.
The second type is sequence, that is, several related data are grouped together in a certain order, also called array or List, for example, "Beijing, shanghai ".
The third type is mapping, that is, a Name/value pair (Name/value). That is, the data has a Name and a corresponding value, this is also called hash or dictionary, for example, "capital: Beijing ".
In programming languages, as long as arrays and objects exist, all data can be stored.
Another difference between an array and an object is that the data in the array does not have a "name" (name), and the object data has a "name" (name ).
JavaScript has five simple data types (also known as basic data types): Undefined, Null, Boolean, Number, and String. There is also a complex data type-Object, which is essentially composed of a group of unordered name-value pairs.
Using the typeof operator for a value may return one of the following strings:
● "Undefined" -- if this value is not defined;
● "Boolean" -- if this value is a boolean value;
● "String" -- if the value is a string;
● "Number" -- if this value is a value;
● "Object" -- if the value is an object or null;
● "Function" -- if this value is a function;
Undefined type:
The 'undefined' type has only one value. When using var to declare a variable but not initializing it,
The value of this variable is undefined.
Null type
Null is the second data type with only one value. This special value is null. Logically, the null value indicates a null object Pointer, which is exactly why "object" is returned when the typeof operator is used to detect null. For example:
The Code is as follows:
Var car = null;
Alert (typeof car); // "object"
Number Type
This type is used to represent integers and floating point values. There is also a special value, that is, NaN (non-value Not a Number ). This value is used to indicate a situation where the operand that originally returns a value does not return a value (this will not throw an error ).
String type
The String type is used to represent a String that consists of zero or multiple 16-bit Unicode characters. A string can be expressed by single quotation marks (') or double quotation marks.
Values, Boolean values, objects, and string values all have the toString () method. But null and undefined values do not have this method.
In most cases, parameters are not required to call the toString () method. However, when calling the toString () method of a value, you can pass a parameter: The base number of the output value.
The Code is as follows:
Var num = 10;
Alert (num. toString (); // "10"
Alert (num. toString (2); // "1010"
Alert (num. toString (8); // "12"
Alert (num. toString (10); // "10"
Alert (num. toString (16); // ""
If you do not know whether the value to be converted is null or undefined, you can also use the transformation function String (), which can convert any type of value to a String. The String () function follows the following conversion rules:
● If the value has a toString () method, call this method (without parameters) and return the corresponding result.
● If the value is null, "null" is returned"
● If the value is undefined, "undefined" is returned"
Object Type
Objects are actually a set of data and functions. An object can be created by executing the new operator and the name of the object type to be created. You can create a custom Object by creating an instance of the Object type and adding its attributes and (or) methods.
Var o = new Object ();
Typeof Operator
The Code is as follows: