JSON (from Ww3school)

Source: Internet
Author: User
Tags javascript eval

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.

{    "Employees":  [        "FirstName": "Bill", "LastName": "Gates" },        " FirstName ":" George "," LastName ":" Bush " },        " firstName ":" Thomas "," LastName ":" Carter " }    ]} 

This employee object is an array that contains 3 employee records (objects).

What is JSON?
    • JSON refers to JavaScript Object notation (JavaScript Notation)
    • JSON is a lightweight text data interchange Format
    • JSON 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 language and platform. The JSON parser and the JSON library support many different programming languages.

Name:<span id= "Jname"></span><br/>Age :<span id= "Jage"></span><br/>Address:<span id= "Jstreet"></span><br/>Phone:<span id= "Jphone"></span><br/></p><script type=" Text/javascript ">var Jsonobject= {"Name": "Bill Gates","Street": "Fifth Avenue New York 666","Age": 56,"Phone": "555 1234567"};d Ocument.getelementbyid ("Jname"). innerhtml=JSONObject.namedocument.getElementById ("Jage"). innerhtml=JSONObject.agedocument.getElementById ("Jstreet"). innerhtml=JSONObject.streetdocument.getElementById ("Jphone"). innerhtml=Jsonobject.phone</script></body>Like XML
 
 
    • JSON is plain text
    • JSON has a "self-descriptive" (Human readable)
    • JSON has a hierarchy (values exist in values)
    • JSON can be parsed with JavaScript
    • JSON data can be transmitted using AJAX
Compared to the difference between XML
    • No end tag
    • Even shorter
    • Read and write faster
    • Ability to parse using the built-in JavaScript eval () method
    • Working with arrays
    • Do not use reserved words
Why use JSON?

For AJAX applications, JSON is faster and easier to use than XML:

Using XML
    • Reading an XML document
    • Using the XML DOM to iterate through a document
    • Read the value and store it in a variable
Using JSON
    • Reading JSON strings
    • Working with the JSON string with eval ()

JSON syntax
    • Introduction to JSON
    • JSON uses

JSON syntax is a subset of JavaScript syntax.

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 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:

"FirstName": "John"

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

FirstName = "John"
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 Object

The JSON object is written in curly braces:

An object can contain multiple name/value pairs:

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

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

FirstName = "John" lastName = "Doe"
JSON Array

The JSON array is written in square brackets:

An array can contain multiple objects:

{"Employees": [{"FirstName": "John", "LastName": "Doe"},{"FirstName": "Anna", "LastName": "Smith"},{"FirstName": " Peter "," LastName ":" Jones "}]}

In the example above, the object "Employees" is an array that contains three objects. Each object represents a record of a person (with a first and last name).

JSON uses JavaScript syntax

Because JSON uses JavaScript syntax, there is no need for additional software to handle JSON in JavaScript.

With JavaScript, you can create an array of objects and assign values like this:

Example
var employees = [{"FirstName": "Bill", "LastName": "Gates"},{"FirstName": "George", "LastName": "Bush"},{"FirstName": "T Homas "," LastName ":" Carter "}];

You can access the first item in an array of JavaScript objects like this:

Employees[0].lastname;

The returned content is:

Gates

You can modify the data like this:

Employees[0].lastname = "Jobs";

JSON file
    • The file type of the JSON file is ". JSON"
    • The MIME type of JSON text is "Application/json"
JSON uses the conversion of JSON text to JavaScript objects

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.

To make it easier for you to explain, we use strings as input for demonstrations (not files).

JSON instance-an object from a string

Create a JavaScript string that contains the JSON syntax:

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

Because JSON syntax is a subset of JavaScript syntax, the JavaScript function eval () can be used to convert JSON text to JavaScript objects.

The eval () function uses the JavaScript compiler to parse the JSON text and then generate the JavaScript object. You must enclose the text in parentheses in order to avoid syntax errors:

var obj = eval ("(" + txt + ")");

To use JavaScript objects in a Web page:

Example
First Name:<span id= "fname" ></span><br/>Last Name:<span id= "lname" ></span><br/> </p> <script type= "Text/javascript" >var txt= ' {' Employees ': [' + ' {' firstName ': ' Bill ', ' lastName ': ' Gates '}, ' + ' {' firstName ': ' George ', ' lastName ': ' Bush '}, ' + ' {' FirstName ": Thomas", "LastName": "Carter"}]} ';var obj= eval ("("+txt+")");d Ocument.getelementbyid ("FName"). innerhtml=obj.employees[1 ].firstname document.getElementById ("LName"). innerhtml=obj.employees[1 ].lastname </script></body>

Try it yourself.

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 Web Software Support
  • Firefox (Mozilla) 3.5
  • Internet Explorer 8
  • Chrome
  • Opera 10
  • Safari 4
  • Jquery
  • Yahoo UI
  • Prototype
  • Dojo
  • ECMAScript 1.5
First Name:<span id= "fname" ></span><br/>Last Name:<span id= "lname" ></span><br/> </p> <script type= "Text/javascript" >var txt= ' {' Employees ': [' + ' {' firstName ': ' Bill ', ' lastName ': ' Gates '}, ' + ' {' firstName ': ' George ', ' lastName ': ' Bush '}, ' + ' {' FirstName ": Thomas", "LastName": "Carter"}]} '; obj = json.parse (TXT); document.getElementById ("FName"). innerhtml=obj.employees[1 ].firstname document.getElementById ("LName"). innerhtml=obj.employees[1 ].lastname </script></body>

JSON (from Ww3school)

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.