JSON instance

<! DOCTYPE HTML > < HTML > <head> <meta charset=" utF-8 "> </title> </head> <body> <h2>JavaScript created JSON object </h2> <p> Website name: < SPAN ID ="jname"></ SPAN ><br /> Website address: < SPAN ID ="jurl"></span><br /> Website slogan: < span id = "jslogan" > < / span > < br / > < / p > < script > var JSONObject = {" name ":" ali YunDaXue ", "url" : "edu.aliyun.com", "Slogan ":" Learn not only technology, but also dream!" }; document.getElementById("jname").innerHTML=JSONObject.name document.getElementById("jurl").innerHTML=JSONObject.url document.getElementById("jslogan").innerHTML=JSONObject.slogan </script> </body> </html>Copy the code

The results

Slogan: Learn not only technology, but also dream!Copy the code

Similarities with XML

L JSON is plain text

L JSON is “self-descriptive” (human-readable)

L JSON has a hierarchical structure (values within values)

L JSON can be parsed through JavaScript

L JSON data can be transferred using AJAX

Differences from XML

L No closing tag

L have a shorter

L Faster reading and writing

L can be parsed using the built-in JavaScript eval() method

L Using arrays

L Do not use reserved words

Why use JSON?

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

Using XML

Reading XML documents

Use the XML DOM to loop through the document

Read the value and store it in a variable

Using JSON

Reading JSON string

Use eval() to process JSON strings

Free JSON lessons:Ali Yun University — Developer Class