JSON: JavaScript Object Notation

JSON is a syntax for storing and exchanging text information. Similar to XML.

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

JSON is a lightweight data interchange format.

It is based on a subset of the JS specification (developed by the European Computer Society) and uses a text format that is completely independent of the programming language to store and represent data. The simplicity and clarity of the hierarchy make JSON an ideal data exchange language.

Easy to read and write, but also easy to machine parsing and generation, and effectively improve the efficiency of network transmission.

directory

 

introduce

Similarities with XML

Differences from XML

JSON syntax rules

The JSON value can be

JSON.parse()

JSON.stringify()

Browser support


introduce

With our editor, you can edit JavaScript code online and view the results with the click of a button:

<! DOCTYPEhtml>
<html>
<head>
<meta charset="utf-8">
<title>Sun Called beast's blog</title>
</head>
<body>
<h2>JavaScript creates a JSON object</h2>
<p>Website Name:<span id="jname"></span><br />Website Address:<span id="jurl"></span><br />Site slogan:<span id="jslogan"></span><br /> 
</p>
<script>
var JSONObject= {
    "name":"Sun's Blog"."url":"http://sunmenglei.blog.csdn.net/"."slogan":"A site to help you become a full stack developer."
};
document.getElementById("jname").innerHTML=JSONObject.name 
document.getElementById("jurl").innerHTML=JSONObject.url 
document.getElementById("jslogan").innerHTML=JSONObject.slogan
</script>
 
</body>
</html>
Copy the code

Similarities with XML

  • JSON is plain text
  • JSON is “self-descriptive” (human-readable)
  • JSON has a hierarchy (values within values)
  • JSON can be parsed through JavaScript
  • JSON data can be transferred using AJAX

Differences from XML

  • No closing tag
  • shorter
  • Faster reading and writing
  • Ability to parse using the built-in JavaScript eval() method
  • Use an array
  • No reserved words are used

JSON syntax rules

JSON syntax is a subset of JavaScript object representation syntax.

  • The data is in the name/value pair
  • Data is separated by commas
  • Braces hold objects
  • Brackets hold arrays

The JSON value can be

  • Numbers (integer or floating point)
  • String (in double quotes)
  • Logical value (true or false)
  • Array (in brackets)
  • Object (in braces)
  • null

 

JSON.parse()

JSON is typically used to exchange data with a server.

It is usually a string when receiving server data.

We can convert data to JavaScript objects using the json.parse () method.

// For example, we received the following data from the server:

  1. { "name":"sunjiaoshou", "alexa":10000, "site":"www.sunjiaoshou.com" }
  2. //Parse () we use the json.parse () method to process the above data and convert it to a JavaScript object:
  3. var obj = JSON.parse('{ "name":"sunjiaoshou", "alexa":10000, "site":"www.sunjiaoshou.com" }');

JSON.stringify()

JSON is typically used to exchange data with a server.

When sending data to the server, it is usually a string.

We can convert JavaScript objects to strings using the json.stringify () method.

// For example, we send the following data to the server:

  1. var obj = { "name":"sunjiaoshou", "alexa":10000, "site":"www.sunjiaoshou.com"};
  2. We use the json.stringify () method to process the above data and convert it to a string:
  3. var myJSON = JSON.stringify(obj);

Browser support

The json.stringify () function is supported by all major browsers:

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