JS Note--json

Source: Internet
Author: User

1. What's the JSON:

JSON refers to JavaScript Object notation (JavaScript object Notation),

JSON is a lightweight text data interchange format;

JSON is independent of language;

JSON is self-descriptive and easier to understand;

JSON uses JavaScript syntax to describe data objects, but JSON is still independent of languages and platforms, and JSON parsers and JSON libraries support many different programming languages.

2.json-convert to JavaScript object:

The JSON text format is syntactically identical to the code that creates the JavaScript object.

Therefore, without a parser, JavaScript programs can use the built-in eval () function to generate native JavaScript objects with JSON data.

3.json Syntax:

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

4.json format: Name/value pairs, name-value pairs include field names (in double quotes), followed by a colon, and then a value

JSON object: {"FirstName": "John", "LastName": "Doe"}

  

JSON array: {"Employees": [{"Flrstname": "John", "LastName": "Doe", "FirstName": "Anna", "LastName": "Smith"}]}

    

Convert 5.json text to JavaScript objects:

The JavaScript function eval () can be used to convert the JSON text to a JavaScript object.

var txt = ' {' Employees ': [        ' + ' {' firstName ': ' Bill ', ' lastName ': ' Gates '},        ' + ' {' firstName ': ' George ', ' lastName ":" Bush "},        ' + ' {" FirstName ":" Thomas "," LastName ":" Carter "}]} '
;
var obj=eval (' (' +txt+ ') ');

<script type= "Etxt/javascript" >
document.getElementById (' fname '). Innerhtml=obj.employees[1].firstname;
document.getElementById (' lname '). Innerhtml=obj.employees[1].lastname;
</script>

(Outside a JSON ' employees ', inside is an array, the inside of the array is JSON,)

6.json Read

1.var json={name: ' Miaov ', Age:23,fun: ' Web '}

for (Var sttr in JSON) {

alert (attr);

}

    

2.var json={' name ': ' Miaov ', ' age ': $, ' fun ': ' Web '}

for (Var sttr in JSON) {

Alert (json[attr]);

}

JS Note--json

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.