JavaScript Learning Summary (vi) data type and JSON format

Source: Internet
Author: User

Transferred from: http://segmentfault.com/a/1190000000668072

What is JSON

JSON: JavaScript Object Notation ( J Ava S cript O bject N otation).

JSONis in the form of a “{}” list of items enclosed in curly braces, separated by commas (,) for each item, and the item is a colon (:) separated property name and property value. This is a typical dictionary representation and again indicates that javascript the object is a dictionary structure. No matter how complex an object is, you can create and assign a value with a single line of JSON code. In JSON , the name/value pair includes the field name (in double quotes), followed by a colon, and then the value

JSON structure

There are two types of JSON structure

jsonIn short, they are javascript objects and arrays, so these two structures are objects and arrays of two structures that can represent a variety of complex structures

1, Object : Object in JS is represented in the “{}” content, data structure is {key:value,key:value,...} the structure of the key value pairs, in the object-oriented language, key is the property of the object, value is the corresponding property value, so it is easy to understand, the value of the method for the object .keyGets the property value, the type of the property value can be a number, a string, an array, several objects.

2, array: the array in JS is enclosed in parentheses “[]” , the data structure is ["java","javascript","vb",...] , the value is the same as in all languages, using index to get, the type of field value can be a number, string, array, object several.
Through the object, the array 2 kinds of structure can be combined into a complex data structure.

JSON Syntax Rules

JSONSyntax is a JavaScript subset of the object notation syntax.

数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组

JSONThe values can be:

数字(整数或浮点数)字符串(在双引号中)逻辑值(true 或 false)数组(在方括号中)对象(在花括号中)null

? In addition to the above 6 kinds, no other, no like JS undefined, NAN

1) 并列的数据之间用逗号(", ")分隔。2) 映射用冒号(": ")表示。3) 并列数据的集合(数组)用方括号("[]")表示。4) 映射的集合(对象)用大括号("{}")表示。

Object is an unordered set of name/value pairs, an object starting with the left branch and ending with the right branch

A value can be a string enclosed in double quotation marks, or a number, one true or false , an array, or an object

JSON and Object literals

In a programming language, a literal is a notation that represents a value.
JSONDouble quotes must be used to specify key-value pairs ! Quotes. For example:

{"first name" : "Mike"};

In a JSON string, you cannot use a function or regular expression literal

Data type:

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

第一种类型是标量(scalar),也就是一个单独的字符串(string)或数字(numbers),比如"北京"这个单独的词。第二种类型是序列(sequence),也就是若干个相关的数据按照一定顺序并列在一起,又叫做数组(array)或列表(List),比如"北京,上海"。第三种类型是映射(mapping),也就是一个名/值对(Name/value),即数据有一个名称,还有一个与之相对应的值,这又称作散列(hash)或字典(dictionary),比如"首都:北京"。

In a programming language, you can store all the data as long as you have arrays and objects (object).

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

JavaScriptThere are 5 simple data types (also known as basic data types): Undefined ,,, Null Boolean , Number and String . There are also 1 complex data types-- Object Object Essentially, a set of unordered name-value pairs.

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

"undefined"——如果这个值未定义;"boolean"——如果这个值是布尔值;"string"——如果这个值是字符串;"number"——如果这个值是数值;"object"——如果这个值是对象或null;"function"——如果这个值是函数;
Undefined type:
    `Undefined`类型只有一个值,在使用var声明变量但未对其加以初始化时,这个变量的值就是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, a null value represents an empty object pointer, which is why "object" is returned when using the TypeOf operator to detect NULL, for example:

var car = null;alert(typeof car); // "object"
Number Type

This type is used to represent integers and floating-point numbers, and there is a special value, Nan (non-numeric not a number). This value is used to indicate that an operand that would have returned a numeric value does not return a numeric value (so that no error is thrown).

String type

  StringThe type is used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, the string. The string can be represented by a single quotation mark (') or double quotation mark (").

There are methods for numeric, Boolean, object, and string values toString() . But null and undefined the value does not have this method.

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

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"

null undefined You can also use a transform function String() , which converts any type of value to a string, without knowing whether the value to be converted is or not. The String() function follows the following translation rules:

If the value has a toString() method, the method is called (no arguments) and the corresponding result is returned

If the value is null , then return " null "

If the value is undefined , then return " undefined "

Object type

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

var o = new Object();
typeof operator
<ScriptType="Text/javascript" > var s ="Nicholas";var B =Truevar C =21;var u;var n =null; var o = {}; var obj = new Object;//Best Write New object () Form alert (typeof s); typeof b); typeof c); typeof u); typeof N); typeof o); typeof obj); </SCRIPT>       
JSON online parsing

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

JavaScript Learning Summary (vi) data type and JSON format

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.