JavaScript Learning Notes (vi) data types and JSON format _ basics

Source: Internet
Author: User

What is JSON

Json:javascript Object Notation (JavaScript object notation).

JSON is in the form of a list of items surrounded by braces "{}", separated by commas (,), and the item is the property name and attribute value separated by a colon (:). This is a typical dictionary representation and once again shows that the object in JavaScript is the dictionary structure. No matter how complex the object is, you can create and assign a value using a JSON code.

JSON structure

JSON has two types of structure

JSON is simply a set of objects and arrays in JavaScript, so these two structures are objects and arrays of two structures that can represent complex structures

1, object: Object in JS is expressed as "{}" enclosed content, data structure is {Key:value,key:value,...} The structure of key-value pairs, in object-oriented language, key is the object's attribute, value is the corresponding attribute value, so it is easy to understand that the value method is the object. Key gets the value of the property, which can be a number, a string, an array, and several objects.

2, array: The array in JS is in the brackets "[]" enclosed content, the data structure is ["Java", "JavaScript", "VB",...], as in all languages, the type of field value can be numbers, strings, arrays, objects, and so on.
Through the object, the array of 2 structure can be combined into a complex data structure.

JSON Syntax rules

The JSON syntax is a subset of the JavaScript object representation syntax.

Data in name/value pairs
Data separated by commas
Curly Braces Save Objects
square brackets to save an array
The JSON value can be:

Number (integer or floating-point numbers)
String (in double quotes)
Logical value (TRUE or FALSE)
Array (in square brackets)
Object (in curly braces)
Null

1 The data is separated by commas (",").

2) The mapping is represented by a colon (":").

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

4 the Mapped collection (object) is represented by braces ("{}").
JSON example

To create an object without any attributes:

Copy Code code as follows:

var obj = {};

Create an object and set properties and initial values:

Copy Code code as follows:

var author = {Name: "Trigkit4", age:21, Sex: "Male"};

Create an object and set properties and methods:

Copy Code code as follows:

var Hello ={content: "How are You?", Say:function () {alert (this.content)}};

Create a nested array of other objects and objects:

Copy Code code as follows:

var company = {Name: "Apple",
Product: "Ipphone",
Chairman:{name: "Tim Cook", age:54},
Employees:[{name: "Jony Ive", Age:47},{name: "Lili", age:29}],
};

Object is a set of unordered name/value pairs, an object begins 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:

Structurally, all data can eventually be decomposed into three types:

The first type is a scalar (scalar), a separate string or number (numbers), such as "Beijing", a separate word.

The second type is the sequence (sequence), in which several related data are tied together in a certain order, also called an array or list, such as "Beijing, Shanghai".

The third type is the mapping (mapping), a name/value pair (Name/value), where the data has a name and a corresponding value, which is also known as a hash (hash) or a dictionary (dictionary), such as "Capital: Beijing".
In a programming language, you can store all of your data with arrays and objects (object).

Another difference between an array and an object is that the data for the array has no name, and the object's data has a name.

There are 5 simple data types (also known as basic data types) in javascript: Undefined, Null, Boolean, number, and string. There are also 1 kinds of complex data types--object,object are 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 this value is not defined;

"Boolean"-if this value is a Boolean value;

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

"Number"-if the value is numeric;

"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, and when the variable is declared with Var but not initialized,
The value of this variable is undefined.
Null type

The null type is the second data type with only one value, and this particular value is null. From a logical point of view, a null value represents an empty object pointer, which is why the "object" is returned when using the TypeOf operator to detect NULL, for example:

Copy Code code as follows:

var car = null;
Alert (typeof car); "Object"

Number Type

This type is used to represent integers and floating-point values, and a special value, that is, Nan (non-numeric not a number). This value is used to indicate that an operand that would otherwise return a numeric value does not return a numeric value (this will not throw an error).

String type

The string type is used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, that is, a string. Strings can be represented by single quotes (') or double quotes (").
Values, Boolean values, objects, and string values all have the ToString () method. However, null and undefined values do not have this method.

In most cases, calling the ToString () method does not have to pass parameters. However, when you call the ToString () method of a value, you can pass a parameter: the cardinality of the output value.

Copy Code code 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

You can also use the transformation function string () When you do not know if the value to convert is null or undefined, and this function can convert any type of value to a string. The String () function follows the following conversion rules:

If the value has the ToString () method, call the method (without parameters) and return the corresponding result

Returns "NULL" if the value is null

Returns "Undefined" if the value is undefined

Object type

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

var o = new Object ();
typeof operator

Copy Code code as follows:

<script type= "Text/javascript" >
var s = "Nicholas";
var b =true;
var C = 21;
var u;
var n = null;
var o = {};
var obj = new object;//best write new Object () Form

Alert (typeof s);//string
Alert (typeof b);//boolean
Alert (typeof c);//number
alert (typeof u);//undefined
Alert (typeof N);//object
Alert (typeof O);//object
Alert (typeof obj);//object
</script>

JSON online parsing

JSON online parsing: http://json.tongxiehui.net/

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.