JSON

1. What is JSON

       -JSON refers to JavaScript Object Notation (Javascript Object Notation)

       -JSON is a lightweight text data interchange format

       - JSON is language independent

       -JSON is self-describing and easier to understand

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

2. Purpose :

       The uses of JSON can be boiled down to the following:

       1. JSON provides an excellent object-oriented approach to caching element data on the client

       2. JSON helps separate validation data and logic

       3. JSON helps provide the essence of Ajax to web applications

Third, the similarities and differences with XML

       Similar to XML:          

  • JSON is plain text
  • JSON is "self-describing" (human readable)
  • JSON has a hierarchical structure (values ​​exist within values)
  • JSON can be parsed by JavaScript
  • JSON data can be transferred using AJAX

       Different from XML:

  • no closing tag
  • Shorter
  • Read and write faster
  • Ability to parse using the built-in JavaScript eval() method
  • use an array
  • Do not use reserved words

4. Why use JSON

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

          Using XML:

                Read XML documents

                Looping through documents using the XML DOM

                read value and store in variable

                Read JSON string

                Processing JSON Strings with eval()

           Using JSON:

                Read JSON string

                Processing JSON Strings with eval()

Five, the use of JSON in Java

      1. Syntax rules: JSON syntax is a subset of Javascript Object Notation syntax. Data is in name/value pairs and separated by commas, {} holds objects, [] holds arrays

      2. Name/value pair: JSON data is written in the format: name/value pair, the name/value pair includes the field name (in double quotes), followed by a colon, and then the value: such as: "firstName" : "John ", which is equivalent to the JavaScript statement firstName="John".

      3. Value: JSON value can be: number (integer or float), string (in double quotes), logical value (true or false), array (in square brackets), object (in curly brackets), null

      4. Objects: JSON objects are written in curly braces, and objects can contain multiple name/value pairs: { "firstName":"John" , "lastName":"Doe" }

      5. Arrays: JSON arrays are written in square brackets, and arrays can contain multiple objects:

{
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
}

6. Examples

       Create a JavaScript string containing JSON syntax:

              vartxt = '{ "sites" : [' +
              '{ "name":"xxx" , "url":"www.xxx.com" },' +
              '{ "name":"yyy" , "url":"www.yyy.com" },' +
              '{ "name":"zzz" , "url":"www.zzz.com" } ]}';

       Since 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 a JavaScript compiler that parses JSON text and produces JavaScript objects. The text must be enclosed in parentheses to avoid syntax errors:

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

       Using JavaScript objects in web pages: Example source code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>xxxyyyzzz</title>
</head>
<body>
<h2>从 JSON 字符串中创建对象</h2>
<p>
网站名: <span id="name"></span><br>
网站地址: <span id="url"></span><br>
</p>
<script>
var txt = '{ "sites" : [' +
'{ "name":"xxx" , "url":"www.xxx.com" },' +
'{ "name":"yyy" , "url":"www.yyy.com" },' +
'{ "name":"zzz" , "url":"www.zzz.com" } ]}';

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

document.getElementById("name").innerHTML=obj.sites[0].name
document.getElementById("url").innerHTML=obj.sites[0].url
</script>
</body>
</html>

         This article refers to: http://www.w3school.com.cn/json/json_eval.asp

                              http://www.runoob.com/json/json-eval.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324593162&siteId=291194637