JSON learning one (basic concept knowledge learning)

Source: Internet
Author: User
Tags tojson

1.Json Brief Introduction

JSON (JavaScript Object Notation)
is a lightweight data interchange format. It makes it easy for people to read and write.

At the same time also facilitates the machine to parse and generate. It is based on JavaScript
Programming Language, Standard ECMA-262 3rd Edition-december 1999
A subset of the. JSON is written in a text format that is completely independent of the programming language. But also used the C language habits (including C, C + +, C #, Java,
JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

JSON is based on two types of structures:

?“ A collection of name/value pairs (a collection of Name/value

In different programming languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary). Hash table (hash
table), with a key list (keyed list). or an associative array (associative array).

The sequence of values (an ordered list of

In most languages, it is implemented as an array (array), vector (vectors), list, sequence (sequence).

These are common data structures. For now, the vast majority of programming languages support them in some form. This makes it possible to exchange data in the same format between various programming languages.
JSON has the following forms:
An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).

An array is an ordered collection of values (value). An array starts with "[" (the left square bracket), and "]" (the right square bracket) ends.

Use "," (comma) to separate values.

The value (value) can be a double-quoted string (string), numeric value (number), True, False, NULL, Object (objects), or array. These structures can be nested.

A string is a set of random Unicode characters enclosed by a double-argument, escaped with a backslash.

A character (character) is a separate string (character string).
The JSON string is very similar to the C or Java string.

The value (number) is also very similar to the value of C or Java.

Only JSON values are not used in octal and hexadecimal format.

At the same time, you can add whitespace between random tags.

JSON in 2.Java

2.1.json Source Code

Source code: Https://github.com/stleary/JSON-java

Use of 2.2.JsonLib
The required jars include the following:
: https://search.maven.org/#search |gav|1|g%3a%22org.json%22%20and%20a%3a%22json%22

JSON in 3.JavaScript

JSON is a subset of the text symbols for JavaScript objects. Because JSON is a subset of JavaScript, it can be used freely in JavaScript.

var  myjsonobject = { "bindings" : [{ "ircevent" :  " Privmsg ", " method ": " Newuri ",  "regex" :  "^http://.*" },{ " Ircevent ": " privmsg ", " method ":  "Deleteuri" ,  "regex" :  ^ delete.* "},{" ircevent ": " privmsg ", Span class= "hljs-string" > "method" :  "Randomuri" ,  "regex" :  "^random.*" }]}; 

In this example, you create an object that includes only a member bindings, and this object includes an array of three objects, each of which includes the Ircevent, method, and regex three members.
Members can be referenced by a dot (.) or subscript ([]) operator.


To convert a JSON string into an object, we can use the eval () function. Eval () triggers the JavaScript compiler. Because JSON is a true subset of JavaScript, the compiler parses the string content correctly and generates an object structure at the same time. Strings must be enclosed in parentheses to avoid JavaScript syntax ambiguity.


The eval function is fast. It compiles and runs no matter what JavaScript code, there may be security issues here. Eval can be used when the source is trustworthy and complete. Using a JSON parser is a more secure approach. In a Web program that uses XMLHttpRequest, it only agrees to communicate in the same origin, so it is trustworthy. But it is not necessarily complete. Assuming that the server is not using a rigorous JSON encoding, or that it does not rigorously check all of the input, it can also provide invalid JSON, with a critical script at the same time. At this point, Eval will run the script, creating a compromise.
To solve the problem, you should use the JSON parser. The JSON parser simply recognizes the JSON text and rejects all scripts. In browsers that provide native JSON support, the JSON parser is faster than eval . Native JSON support is very likely to be included in the standard ECMAScript the next version number.

varJSON.parse(myJSONtext, reviver);

The optional reviver parameter is a function that will be called after each key/value is finally turned on at each level. Each value will be replaced with the return result of the Reviver function. It can be used to organize objects into an instance of a pseudo-class (pseudoclasses), or to convert a date string into a Date object

MyData = Json.parse (text,function(Key,value) {var type;if(value&& typeofvalue===' object ') { Type = value.type;if(typeof Type ===' String '&& typeof window[ type] ===' function ') {returnNew(window[ type]) (value); }}returnvalue; });

JSON serialization is a reverse action that transforms the data structure of JavaScript into a JSON string. JSON does not support looping data structures, so when using JSON strings, do not give it a circular reference structure.

varJSON.stringify(myObject, replacer);

Suppose the Stringify method discovers an object that includes a ToJSON method that calls this method and then strings the returned result. This agrees that the object customizes their JSON representation.
The string method is capable of taking an optional array of strings. These strings can be used to select which properties will be included in the generated JSON text.
The string method can also use an optional replacer function that will be called after the ToJSON method of each value in the structure (assuming that it exists). It will receive each key and value as its number of parameters. This is bound to the object that includes the key. The value it returns is string-formatted.
Values that are not represented by JSON (such as functions and undefined) will be excluded.
Infinitely large numbers will be replaced with NULL, and to replace other values, you can use similar replacer functions:

valueif (typeofvalue‘number‘ && !isFinite(valuereturn String(value); }returnvalue

Providing a suitable reviver function to Json.parse can be achieved.

4.Json and XML

Extensible Markup Language (XML) is a text format that comes from standard Generalized Markup Language (SGML). XML is simpler than SGML. Hypertext Markup Language (HTML), in contrast, is much simpler. Nonetheless, a good HTML reference book is an inch thick. This is because the formatting and structuring of documents is a complex matter.
The advantage of most XML is the role that surrounds it as an interactive data sequence format. XML has two great advantages as a data representation language:
-It is text-based.
-It is location independent.
All of this makes her independent of the application at a higher level than other data interchange formats. In fact, XML is already a standard for the most, which means there is no need to argue (or look at it) anymore.
Unfortunately, XML is not very well suited for data exchange, as if a wrench is not used to nail nails. It includes too many wrappers, and it does not match the data model of most programming languages. Most program apes were shocked by the ugliness and inefficiency of XML at the first sight of XML. It turns out that the first reaction is right. There is also a mosquito symbol, she not only has the full advantage of XML, and she is more suitable for data exchange. This symbol is the JavaScript object symbol (JSON).
The most sensible insight into XML (for example: xmlsuck.org) indicates that XML has a very large problem as a form of data interaction. But this shortcoming is made up by the interoperability and openness it brings.
JSON has the same interoperability and openness, and there are no other deficiencies.
Let's compare XML and JSON with attributes that we think are important.
1) Simple

2) Scalability
JSON is not extensible, because she does not need to be expanded. JSON is not a document markup language, so there is no need to define new tags or attributes to represent internal data.
3) Interoperability
JSON has the same interoperability performance as XML.
4) Openness
JSON is at least as open as XML and perhaps more open. Because she is not at the center of the Community/political standardization struggle.
W) XML is available for human reading.
JSON is easier to read than XML. She is also more easy to write. At the same time, it is also easier to be read and written by the machine.
6) XML can be used as a data interchange format, allowing users to move their data between similar programs.
The same is true for JSON.
7) XML provides a data structure to include a lot of other information.
The same is true for JSON.
8) XML is very easy to handle, because the structure of the data is simple and standard.
JSON is easier to handle, because her structure is simpler.
9) There are a lot of reusable software available to program apes to process XML, and they don't need to rewrite the code.
JSON, as a simpler symbol, requires less software. In JavaScript and Python, JSON notation is built into the language of the program. There is no need for any extra software. In other languages, only a small amount of JSON-specific code is required. For example, Json.org provides a package that includes only three classes to enable the Java language to support JSON.
10) XML separates the representation of data from the structure of the data
XML needs to transform the data structure into a document structure. This mapping is complex. The JSON structure is based on arrays and records. That is the composition of the data. The XML structure is based on elements (able to nest), attributes (cannot be nested), primitive mosquitoes, entities, DTDs, and other meta-data structures.
11) A general-purpose interchange Format
JSON is a better format for exchanging data. XML is a better format for document exchange. Do the right thing with the right tools.
12) A multi-data display
JSON does not provide whatever display capability because she is not a document markup language.
13) Self-descriptive narrative data
XML and JSON are the same at this point.
14) fully integrated with all traditional databases and formats
(Statements about XML are sometimes a bit exaggerated) XML documents can include data types that can be imagined-from traditional data, such as text and numbers, to multimedia objects such as sounds, to active formats, such as Java applets or ActiveX components.
JSON has no <[CDATA[]]> features, so she is not suitable for carrying sounds or images or other large binary loads. The JSON is optimized for the data. In addition, providing a running program in a data exchange system can introduce critical security issues.
15) Internationalization
Both XML and JSON are encoded in Unicode.
16) Openness and scalability
XML has an open structure that will allow you to add artistic elements to other countries when you need them. This means that you can adjust your system at any time to accept the vocabulary of a particular industry.
These words can be converted to JSON on their own initiative, and migrating from XML to JSON is straightforward.
17) XML is easy to read for both humans and machines.
JSON is easier for people and machines to read than XML.
18) XML is Object-oriented
In fact, XML is document-oriented. JSON is data-oriented. JSON can be easier to map to an object-oriented system.
19) XML is widely used by the computer industry
JSON has just been known to all. Her simplicity and the very easy transition from XML to JSON make JSON more widely available.

JSON learning one (basic concept knowledge learning)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.