JSON Learning Notes

Source: Internet
Author: User

A. the JSON(JavaScript object Notation)javascript objects notation, is a data format (lightweight), Is that storing and exchanging textual information is not a programming language, nor does it belong to JavaScript, and many programming languages have parsers and serializers for JSON.

two. Syntax:

JSON has 6 Types of Values: Object, array, string, number, Boolean,null

Simple values: String, Numeric, Boolean, and null , for example:5, "hello Word"

Object: A set of unordered key-value pairs in which the value in each key-value pair can be a simple value or a value of a complex data type. object has no variables and semicolons, for example:{

"name":"xiyin",

"sex":" female "

}

Arrays: A complex data type that represents a list of ordered sets of values that can be accessed by a numeric index. Arrays also have no variables and semicolons for example:[+,"hi", True]

Arrays and objects can also be combined:

[

{

" < span> : "

"author":"xiyin",

year:2011

},

" < span> :

"author":"xiyin",

year:2015

}

]

JSON does not support variables, functions, or object instances, which are a format for representing structured data.

JSON indicates that a string must use double quotation marks (single-quote pairs cause syntax errors), which is the maximum difference from a JavaScript representation of a string.

four basic rules for using JSON :

(1) separate data with commas

(2) The map is represented by a colon

(3) arrays are denoted by brackets

(4) object with curly braces

three. parsing and serialization

(1)json.stringify () can be used to serialize an object into a JSON string , It receives three parameters:

First: The javascript object to serialize

The second one: the expression is a filter, can be an array or it can be a function

The third: Is an option that indicates whether indentation is preserved in the JSON string

Stringify (), serializes a JavaScript object into a json string,json.stringify () output of JSON The string does not contain any space characters or indents.

For example:

(1) var book = {   "title": "JavaScript",   "authors": "Xiyin",    year:2015};    var jsontext=json.stringify (book);       console.log (Jsontext);    results for:           {"title": "JavaScript", "Author": "Xiyin", "Year":} (2) var book = {   "title": " JavaScript ",  " authors ":" Xiyin ",   year:2015};var jsontext = json.stringify ( book,["title", "Year"]); Console.log (jsontext); The result is:        {"title": " JavaScript "," Year ":2016}  (3) var book = {  " title ":" JavaScript ",  " Authors ":" Xiyin ",   year:2015};var jsontext = json.stringify (book,null,4); Console.log ( Jsontext); The result is:{     "title":  "JavaScript",     "authors":  " Xiyin ",    " Year ": &NBSP;2015} 

( 2 ) Json.parse () method is used to JSON the data resolves to JavaScript object.

Parse () to parse the JSON string into native javascript values

For example: Var bookcopy=json.parse (Jsontext);             Console.log (bookcopy);             The result is: Object {tittle: "JavaScript", Author: "Xiyin", year:2015} console.log (bookcopy["title"]); The result is: JavaScript

Operating under the console:

650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/7F/57/wKioL1caw0rC_PYvAAB3rT1k6kY990.jpg "title=" 3.jpg " alt= "Wkiol1caw0rc_pyvaab3rt1k6ky990.jpg"/>


This article is from the "dream need to adhere to" blog, please be sure to keep this source http://xiyin001.blog.51cto.com/9831864/1766966

JSON Learning Notes

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.