JSON Learning Notes

Source: Internet
Author: User
Tags ming

Json:javascript Object Notation (JavaScript object Notation).

JSON is the syntax for storing and exchanging textual information. Similar to XML.

JSON is smaller, faster, and easier to parse than XML.

JSON Syntax rules

JSON syntax is a subset of the JavaScript object notation syntax.

    • Data in name/value pairs
    • Data is separated by commas
    • Curly braces Save Object
    • Square brackets Save Array
JSON file
    • The file type of the JSON file is ". JSON"
    • The MIME type of JSON text is "Application/json"

JSON Name/value pairs

The writing format for JSON data is: name/value pairs.

Name/value pairs include the field name (in double quotation marks), followed by a colon, and then the value:

"Name": "Li Ming"

This is easy to understand and is equivalent to this JAVASCRIPT statement:

name = "Li Ming"
JSON value

The 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

JSON format Data representation method

The JSON uses curly braces to represent the object, using square brackets to represent the array. The combination of the two can express data with complex structure.

For example:

{key:value,key2:value2}[1,2,3,4,4,4,4,5,6,76]
[{"Name": "Li Ming", "Age": 18},{"name": "Li Long", "Age": 19},{"name": "Li Bai", "Age": 17}]

Examples of programs:

var myjson = [{"Name": "Li Ming", "age": + }            ,"name": "Li Long", "age": + }            ,"name": "Li Bai", "Age": 17
   
     }];        Alert (myjson[0
    ]. Name);        Read name        myjson[3]. name = "Li Bai"
    ;       Change name        alert (myjson[2]. Name);        Read name
   

The results of the operation are as follows:

Because the name of the third object was changed during the run, Li Bai became Li Bai.

Convert JSON text to JavaScript object

One of the most common uses of JSON is to read the JSON data (as a file or as a HttpRequest) from the Web server, convert the JSON data to a JavaScript object, and then use that data in the Web page.

Parsing JSON

You can use the Eval () function to parse JSON data

Example code:

var str= ' [{' name ': ' Li Ming ', ' age ': ' + '}, ' +            ' {' name ': ' Li Long ', ' age ': ' + ', ' +        ' {' name ': ' Li Bai ', ' age ': +}] ';             var obj = eval (str);

The results of the operation are as follows (the results are monitored in the developer tools of IE11):

JSON Parser

Tip: The eval () function compiles and executes any JavaScript code. This hides a potential security issue.

Using the JSON parser to convert JSON to JavaScript objects is a safer practice. The JSON parser only recognizes JSON text and does not compile the script.

In the browser, this provides native JSON support, and the JSON parser is faster.

Native JSON support is included in newer browsers and the latest ECMAScript (JavaScript) standards.

Web Browser Support
Firefox (Mozilla) 3.5

    • Internet Explorer 8
    • Chrome
    • Opera 10
    • Safari 4

Web Software Support

    • Jquery
    • Yahoo UI
    • Prototype
    • Dojo
    • ECMAScript 1.5

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.