JSON introduction
JSON: JavaScript Object Notation(JavaScript Object notation )
JSON Is the syntax for storing and exchanging text information , similar XML.
JSON Than XML smaller 、 faster , Easier to parse .
JSON example
{ “sites”: [ { “name”:” Novice tutorial ” , “url”:”www.runoob.com” }, { “name”:”google” , “url”:”www.google.com” }, { “name”:” Microblogging ” , “url”:”www.weibo.com” } ] }
This sites The object is to contain 3 Site records ( object ) Array of .
What is? JSON ?
- JSON refer to JavaScript Object notation (JavaScript Object Notation)
- JSON It’s a lightweight text data exchange format
- JSON Independent of language :JSON Use Javascript Syntax to describe data objects , however JSON Still independent of language and platform .JSON Parser and JSON The library supports many different programming languages . There’s a lot of movement right now (PHP,JSP,.NET) Programming languages support JSON.
- JSON Be self descriptive , Easier to understand
JSON – Convert to JavaScript object
JSON Text format in syntax and create JavaScript The code of the object is the same .
Because of this similarity , No parser required ,JavaScript The program can use built-in eval() function , use JSON Data to generate native JavaScript object .
Online examples
Through our editor , You can edit it online JavaScript Code , Then click a button to see the results :
JSON example
<!DOCTYPE html> <html> <head> <meta charset=”utf-8″> <title> Novice tutorial (runoob.com)</title> </head> <body> <h2>JavaScript establish JSON object </h2> <p> Website name : <span id=”jname”></span><br /> Web site address : <span id=”jurl”></span><br /> Website slogan: <span id=”jslogan”></span><br /> </p> <script>
var JSONObject= { “name”:” Novice tutorial “, “url”:”www.runoob.com“, “slogan”:” It’s not just technology that you learn , Is the dream !” }; document.getElementById(“jname”).innerHTML=JSONObject.name document.getElementById(“jurl”).innerHTML=JSONObject.url document.getElementById(“jslogan”).innerHTML=JSONObject.slogan
</script> </body> </html>
Try it »
Click on ” Try it ” Button to view the online instance .
And XML Similarities
- JSON Is the plain text
- JSON have ” self-description “( Human readable )
- JSON It has a hierarchy ( There is a value in the value )
- JSON It can be done by JavaScript To analyze
- JSON Data available AJAX transmitted
And XML The difference
- No end tag
- shorter
- Faster reading and writing
- Be able to use built-in JavaScript eval() Method
- Using arrays
- Reserved words are not used
Why use JSON?
about AJAX For applications ,JSON Than XML Faster and easier to use :
Use XML
- Read XML file
- Use XML DOM To loop through the document
- Read the value and store it in the variable
Use JSON
- Read JSON character string
- use eval() Handle JSON character string
JSON and XML All for receiving web Data on the server .
JSON and XML There are differences in writing , As shown below :
JSON example
{ “sites”: [ { “name”:” Novice tutorial ” , “url”:”www.runoob.com” }, { “name”:”google” , “url”:”www.google.com” }, { “name”:” Microblogging ” , “url”:”www.weibo.com” } ] }
XML example
<sites> <site> <name> Novice tutorial </name> <url>www.runoob.com</url> </site> <site> <name>google</name> <url>www.google.com</url> </site> <site> <name> Microblogging </name> <url>www.weibo.com</url> </site> </sites>
JSON And XML The similarities :
- JSON and XML The data are all ” Self description ” , Are easy to understand .
- JSON and XML The data has a hierarchical structure
- JSON and XML Data can be used by most programming languages
JSON And XML The difference :
- JSON There is no need to end the tag
- JSON Shorter
- JSON Faster reading and writing
- JSON You can use arrays
The biggest difference is :XML Need to use XML Parsers to parse ,JSON You can use standard JavaScript Function to parse .
Why? JSON Than XML Better ?
XML Than JSON It’s harder to parse .
JSON You can use the existing JavaScript Object parsing .
in the light of AJAX application ,JSON Than XML Data loads faster , And it’s simpler :
Use XML
- obtain XML file
- Use XML DOM Iterative loop document
- Then the data is parsed and copied to the variable
Use JSON
- obtain JSON character string
- JSON.Parse analysis JSON character string
Related articles
Read more here: Source link