Json nature

Json is essentially a string of complex data containing primitive, string, or primitive plus string. Then there are network requests, often using JSON to transfer data.

JSON is a lightweight data interchange format that leaves subject-verb-object: JSON is the format.

var obj = {a: 'Hello', b: 'World'};
var json = '{"a": "Hello", "b": "World"}'; 
var jsonobj = {"a": "Hello", "b": "World"}
Copy the code

JSON(JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript (the JS specification of 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.

Chinese name JavaScript object simple notation

Foreign name JavaScript Object Notation

Suo write JSON

Majored in computer engineering

A brief history

JSON(JavaScript Object Notation) is a lightweight data interchange format. Easy to read and write. It is also easy for machine parsing and generation. It is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition – December 1999. JSON, a data format popularized by Douglas Crockford in 2001, became a mainstream data format in 2005-2006, when Yahoo and Google began using it extensively.

JSON syntax

JSON syntax rules

JSON is a sequence of tags. This set of tokens contains six construction characters, strings, numbers, and three literal names. JSON is a serialized object or array.

1. Six construction characters:

begin-array = ws %x5B ws ; [begin-object = ws %x7B ws; {end-array = ws %x5D ws;] End -object = ws %x7D ws; } close curly parenthesis name-separator = ws %x3A ws; Separator = ws %x2C ws; The commaCopy the code

2. Allow meaningless whitespace (ws) before or after these six construction characters:

}; % x09 / space; Horizontal label %x0A /; Newline or newline %x0D); The return tripCopy the code

JSON value:

3.1 COMPOSITION of JSON: WS Value WS

3.2 A value can be an object, array, number, string, or one of three literals (false, NULL, or true). The English in literals in values must be lowercase.

3.2.1 Objects consist of comma-separated members enclosed in curly braces. Members are string keys and the values described above consist of comma-separated key-value pairs, such as:
{"name": "John Doe", "age": 18, "address": {"country" : "china", "zip-code": "10000"}}
Copy the code
3.2.2 An array is a set of values enclosed in square brackets, such as:
[3, 1, 4, 1, 5, 9, 2, 6]
Copy the code
3.2.3 Strings are very similar to C or Java strings. A string is a collection of any number of Unicode characters surrounded by double quotes, escaped using a backslash. A character is a single character string.
3.2.4 Numbers are also very similar to C or Java numbers. Remove octal and hexadecimal formats that are not used. Remove some coding details.

Some examples of valid JSON:

{" a ": 1," b ": [1, 2, 3]} [1, 2," 3 ", 4} {" a ":] 3.14" plain_text"Copy the code

Relationship between JSON and JS objects

Many people don’t understand the relationship between JSON and JS objects, or even who is who. JSON is a string representation of a JS object. It uses text to represent information about a JS object, essentially a string. Such as

obj = {a: 'Hello', b: 'World'}; Var json = '{"a": "Hello", "b": "World"}'; // This is a JSON string, essentially a stringCopy the code

JSON and JS objects interrotate

To convert from a JSON string to a JS object, use the json.parse () method:

var obj = JSON.parse('{"a": "Hello", "b": "World"}'); {a: 'Hello', b: 'World'}Copy the code

To convert from a JS object to a JSON string, use the json.stringify () method:

var json = JSON.stringify({a: 'Hello', b: 'World'}); {"a": "Hello", "b": "World"}'Copy the code

Commonly used type

Any of the supported types can be represented by JSON, such as strings, numbers, objects, arrays, and so on. But objects and arrays are special and commonly used types.

{key1: value1, key2: value2,… } key value pair structure. In object-oriented languages, key is an attribute of an object and value is the corresponding value. Key names can be represented as integers and strings. The value can be of any type.

Array: An array in JS is the content wrapped in square brackets [], and the data structure is [” Java “, “javascript”, “VB “,… Index structure of. In JS, arrays are a special data type that can also use key-value pairs like objects, but still use indexes more. Again, the type of the value can be any type.

Based on the sample

In simple terms, JSON converts a set of data represented in a JavaScript object into a string, which can then be easily passed around the network or between programs and restored to a data format supported by each programming language as needed, such as PHP, You can restore JSON to an array or a basic object. When using AJAX, if you need to pass values from an array, you need to convert the array to a string using JSON.

Said object

The object is an unordered collection of name/value pairs. An object begins with {open parenthesis and ends with} close parenthesis. Each name is followed by a colon; Name/Value pairs are separated by commas.

{"firstName": "Brett", "lastName": "McLaughlin"}  
Copy the code

Said array

Like normal JS arrays, JSON represents arrays using square brackets [].

{ 
"people":[ 
{
"firstName": "Brett",            
"lastName":"McLaughlin"        
},      
{        
"firstName":"Jason",
"lastName":"Hunter"
}
]
}
Copy the code

It’s not hard to understand. In this example, there is only one variable named people, and the value is an array of two entries, each of which is a person’s record, containing first and last names. The above example shows how records can be combined into a value using parentheses. Of course, you can use the same syntax to represent a larger number of values (each containing multiple records). There are no predefined constraints to follow when working with JSON-formatted data. So, within the same data structure, you can change the way you represent the data, or you can use different ways to represent the same thing. As mentioned earlier, in addition to objects and arrays, you can also simply use strings or numbers to store simple data, but that doesn’t make much sense.

Comparison with XML

readability

JSON and XML are comparable in readability, with the simple syntax on the one hand and the formal tag form on the other, making it hard to tell.

scalability

XML is naturally extensible, and JSON certainly is, and there is nothing that XML can scale that JSON can’t. However, JSON is the home of Javascript and can store Javascript composite objects, which has an advantage over XML.

Coding difficulty

XML has a wealth of coding tools, such as Dom4j, Dom, SAX, etc. JSON also has tools. Without tools, skilled developers can write XML documents and JSON strings as quickly as they want, but XML documents have many more structural characters.

Decoding the difficulty

XML can be parsed in two ways:

1. Document model parsing, that is, indexing a set of tags by parent tags. For example: xmlData. GetElementsByTagName (” tagName “)

However, this is to be used in the context of knowing the document structure in advance and cannot be used for general encapsulation.

2. Iterate over nodes (Document and childNodes).

This can be done recursively, but the parsed data is still in a variety of forms, and often does not meet the preconceived requirements.

Any such extensible structured data must be difficult to parse.

The same is true for JSON. Using JSON for data transfer is a great way to write useful, beautiful, and readable code if you know the structure in advance. If you’re a pure foreground developer, you’ll love JSON. Not so much if you’re an application developer, though, because XML is really a structured markup language for data transfer. Parsing JSON without knowing its structure is a nightmare. Even if it takes time and effort, the code will become redundant and the results will not be satisfactory. But that doesn’t stop many front-office developers from choosing JSON. Because toJSONString() in json.js shows the string structure of JSON. Of course, this is still a nightmare for people who don’t use this string very often. People who use JSON often see this string and have a good understanding of the structure of JSON, making it easier to manipulate JSON. This is just parsing XML and JSON for data passing in Javascript. JSON, after all, is the home turf of Javascript, and certainly has far more advantages than XML. I’m sure many programmers would cry to parse JSON if they stored Javascript composite objects in JSON and didn’t know their structure. In addition to the above, another big difference between JSON and XML is the effective data rate. JSON is more efficient when transmitted as a packet format because it does not require strict closing tags as XML does, which greatly increases the effective data amount compared to total packets, thus reducing network transmission pressure for the same amount of data traffic.

Compare the

XML and JSON both use structured approaches to mark up data, so let’s do a simple comparison. Data of some provinces and cities in China expressed in XML are as follows:

<? The XML version = "1.0" encoding = "utf-8"? > <country> <name> <province> <name> <cities> <city> <city> <city> <province> <province> <name> <city> <city> <city> <city> <city> <city> <city> <city> <city> <province> <city> <city> <city> <city> <city> <province> <name> Taiwan </name> < City > Taipei </city> <city> Kaohsiung </city> </city> </province> </province> </name> </city> </city> </province> </country>Copy the code

Expressed in JSON as follows:

{" name ":" Chinese ", "province" : [{" name ":" heilongjiang province ", "cities" : {" city ": [" Harbin", "daqing"]}}, {" name ":" guangdong ", "cities" : {" city ": [" guangzhou", "shenzhen", "zhuhai"]}}, {" name ":" Taiwan ", "cities" : {" city ": [" Taipei", "kaohsiung"]}}, {" name ":" xinjiang ", "cities" : {"city": [" city"]}}]}Copy the code

As you can see, JSON’s simple syntax and clear hierarchy are significantly easier to read than XML, and because JSON uses far fewer characters than XML in terms of data exchange, the bandwidth required to transfer data can be significantly reduced.

Check tools

preface

JSON format replaces XML to bring great convenience to network transmission, but it does not have XML at a glance, especially when JSON data is very long, we will fall into the tedious and complex data node search. But Chinese online tools BeJson, SoJson online tools let many programmers, new contact with JSON format programmers faster to understand the structure of JSON, faster accurate positioning of JSON format errors.

function

1. JSON formatting verification

Many people get JSON data, temporarily have no way to determine whether the JSON data format is correct, whether fewer or more symbols caused by the program can not parse, this function just can help you to complete the JSON format verification.

2. JSON view

Most likely a lot of programmers will encounter when looking for a node, will find that if directly to a row of data do not know how to start, even if know which position, but also a node a node to find down, all one not careful and have to start from scratch to find trouble. With this capability, all JSON data is converted into a view format, and you can see at a glance how many arrays are under what objects and how many objects are under an array. This feature is very useful. There are not only view functions but also formatting, compression, escaping and verification functions. All in all, powerful.

3. Compressed escape

When programmers write JSON statement test cases, they often directly write a JSON string for convenience, but they fall into endless trouble of double quotation escape. This compression and escape feature lets you write test cases like a duck in water.

JSON online editor

If you don’t have a familiar editor installed on your current computer, this feature can be useful if you want to modify the JSON data to a specific node.

5. Send JSON data online

As you all know, the most common use of JSON is in web project development, so if you want to test whether an interface can accurately accept JSON data, you have to write a page that sends JSON strings, and do the same thing over and over again. With this feature, you can get rid of writing test pages, because this feature can send the specified JSON data to the specified URL.

6. JSON coloring

When writing a document, many people expect it to be self-explanatory, but it’s ok to look at JSON data with black text on a white background and not be excited. With this feature, all keywords are colored and the data structure is self-explanatory.

7. Json-xml interchange

As the name implies, converting JSON-formatted data to XML, or XML-formatted data to JSON, is not a problem.

8, JSON – VIEW

JSON viewing utility that formats and views JSON data during development (on Windows).

9. It is a data interchange format like XML

JSON minimization

Ralf Sternberg, a committer and leader of Eclipse RAP, put together a fast and lightweight library in just ten classes. Clearly, using lean and analytical methods really improves server performance, as server processes create JSON information for a large number of customers with greater efficiency. There are no dependencies in external JSON, the code is easy to manage, and it doesn’t take a lot of memory. This isn’t nearly enough for your entire JSON project, but it does bring several good things.

WEB technology

HTML

XHTML/HTML 5 CSS/TCP/IP

XML

XML/XSL/XSLT/XSL-FO/XPath/XPointer/XLink/DTD/XML Schema/DOM/XForms/SOAP/WSDL/RDF/RSS/WAP Web Services

Web scripting

JavaScript/HTML DOM/DHTML/VBScript/AJAX/jQuery/JSON/E4X/WMLScript

Serv script

SQL/ASP/ADO/PHP

.NET

Microsoft.NET forced the.net Mobile

multimedia

SMIL forced the SVG