Home > Web Front-end > JS Tutorial > JavaScript study notes (6) Data types and JSON format_Basic knowledge

JavaScript study notes (6) Data types and JSON format_Basic knowledge

WBOY
Release: 2016-05-16 16:34:47
Original
1452 people have browsed it

What is JSON

JSON: JavaScript Object Notation.

The format of JSON is a list of items surrounded by curly brackets "{}", each item is separated by a comma (,), and the item is the attribute name and attribute value separated by a colon (:). This is a typical dictionary representation, and it once again shows that objects in JavaScript are dictionary structures. No matter how complex the object is, it can be created and assigned with a JSON code.

JSON structure

JSON has two structures

Json simply means objects and arrays in JavaScript, so these two structures are objects and arrays. Various complex structures can be expressed through these two structures

1. Object: The object is represented in js as the content enclosed by "{}". The data structure is the key-value pair structure of {key: value, key: value,...}. In object-oriented language , key is the attribute of the object, and value is the corresponding attribute value, so it is easy to understand. The value method is object.key to obtain the attribute value. The type of this attribute value can be numbers, strings, arrays, and objects.

2. Array: Array in js is the content enclosed by brackets "[]", the data structure is ["java", "javascript", "vb",...], the value method and all languages Same as in, using index to obtain, the type of field value can be number, string, array, object.
Through the two structures of objects and arrays, complex data structures can be combined.

JSON syntax rules

JSON syntax is a subset of JavaScript Object Notation syntax.

Data in name/value pairs
Data separated by commas
Curly braces save objects
Square brackets save arrays
JSON value can be:

Number (integer or floating point)
String (in double quotes)
Logical value (true or false)
Array (in square brackets)
Object (in curly braces)
null

1) Parallel data are separated by commas (", ").

2) Mapping is represented by colon (": ").

3) The collection (array) of parallel data is represented by square brackets ("[]").

4) The mapped collection (object) is represented by curly brackets ("{}").
JSON example

Create an object without any properties:

Copy code The code is as follows:

var obj = {};

Create an object and set properties and initial values:

Copy code The code is as follows:

var author = {name: "trigkit4", age: 21, sex: "male"};

Create an object and set properties and methods:

Copy code The code is as follows:

var hello ={content:"how are you?" , say :function(){alert(this.content)} };

Create a nested array of other objects, objects, etc.:

Copy code 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 an unordered set of 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 quotes, or a numeric value, a true or false, an array or an object

Data type:

From a structural point of view, all data can ultimately be decomposed into three types:

The first type is scalar, which is a single string or numbers, such as the single word "Beijing".

The second type is sequence, that is, several related data are arranged together in a certain order, also called array or list, such as "Beijing, Shanghai".

The third type is mapping, which is a name/value pair, that is, the data has a name and a corresponding value, which is also called hash. ) or dictionary, such as "Capital: Beijing".
In programming languages, as long as there are arrays and objects, all data can be stored.

Another difference between arrays and objects is that array data does not have a "name", while object data has a "name".

There are 5 simple data types (also called basic data types) in JavaScript: Undefined, Null, Boolean, Number and String. There is also a complex data type - Object. Object is essentially composed of a set of unordered name-value pairs.

Using the typeof operator on a value may return one of the following strings:

 ● "undefined" - if the value is undefined;

 ● "boolean" - if the value is a Boolean value;

 ● "string" - if the value is a string;

 ● "number" - if the value is a numerical value;

 ● "object" - if the value is an object or null;

 ● "function" - if the value is a function;

Undefined type:

The `Undefined` type has only one value. When using var to declare a variable but not initialize it,
The value of this variable is undefined
Null type

The Null type is the second data type with only one value, and this special value is null. From a logical point of view, the null value represents a null object pointer, and this is why "object" is returned when using the typeof operator to detect null, for example:

Copy code 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, and there is also a special value, NaN (Not a Number). This value is used to indicate that an operand that is supposed to return a value does not return a value (so that an error is not thrown).

String type

The String type is used to represent a character sequence consisting of zero or more 16-bit Unicode characters, that is, a string. Strings can be represented by single quotes (') or double quotes (").
Numeric, Boolean, object and string values ​​all have toString() method. But null and undefined values ​​do not have this method.

In most cases, there is no need to pass parameters when calling the toString() method. However, when calling the toString() method of a value, you can pass a parameter: the base of the output value.

Copy code 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)); //"a"

When you don’t know whether the value to be converted is null or undefined, you can also use the conversion function String(). This function can convert any type of value into 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, return "null"

 ● If the value is undefined, return "undefined"

Object type

An object is actually a collection of data and functions. Objects can be created by executing the new operator followed by the name of the type of object to be created. You can create a custom object by creating an instance of the Object type and adding properties and/or methods to it.

var o = new Object();
typeof operator

Copy code The code is as follows:


Json online parsing

Json online analysis: http://json.tongxiehui.net/

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template