JSON learning one (basic concept knowledge learning)

Source: Internet
Author: User
Tags tojson

1.Json Introduction

JSON (JavaScript Object Notation)
is a lightweight data interchange format. It makes it easy for people to read and write. It also facilitates the analysis and generation of machines. It is based on JavaScript
Programming Language, Standard ECMA-262 3rd Edition-december 1999
A subset of the. JSON takes a completely language-independent text format, but it also uses the C-C language, including
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
Pairs). In different programming languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash
table), a list of keys (keyed list), or an associative array (associative array).

The sequence of values (an ordered list of
Values). In most languages, it is implemented as an array (array), vector (vectors), list, sequence (sequence).

These are common data structures. Currently, most programming languages support them in some form. This makes it possible to exchange data in the same format between various programming languages.
The JSON has these 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 begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.

The value (value) can be a string enclosed in double quotation marks (string), a numeric value (number), True, false, NULL, an object, or an array. These structures can be nested.

A string is a collection of any number of Unicode characters surrounded by double quotation marks, 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 white space between any tags.

JSON in 2.Java

2.1.json Source Code

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



Use of 2.2.JsonLib
json-lib:http://json-lib.sourceforge.net/
The required jar contains the following:
Commons-beanutils-1.8.3.jar
Commons-collections-3.2.1.jar
Commons-lang-2.6.jar
Commons-logging-1.1.1.jar
Ezmorph-1.0.6.jar
Json-lib-2.4-jdk15.jar
2.3.org.json
: 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, an object containing only one member bindings is created, and this object contains an array of three objects, each of which contains the Ircevent, method, and regex three members.
Members can be referenced by a dot (.) or subscript ([]) operator.

 myJSONObject.bindings[0].method"newURI"

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. Strings must be enclosed in parentheses to avoid JavaScript syntax ambiguity.

vareval(‘(‘‘)‘);

The eval function is fast. It compiles and executes any JavaScript code, and 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, only communication is allowed in the same origin, so it is trustworthy. But it is not necessarily complete. If the server is not using a rigorous JSON encoding, or if it does not strictly check all the inputs, it can also provide invalid JSON, along with dangerous scripts. At this point, Eval will execute the script, creating a compromise.
To solve this problem, you should use the JSON parser. The JSON parser will only recognize the JSON text and reject all scripts. In browsers that provide native JSON support, the JSON parser is faster than eval . Native JSON support is likely to be included in the standard for the next version of ECMAScript.

varJSON.parse(myJSONtext, reviver);

The optional reviver parameter is a function that will be called after each key/value is the final result of 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);

If the Stringify method discovers that the object contains a ToJSON method, it calls the method and then strings the returned result. This allows the objects themselves to define their JSON representations.
The string method can take 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 (if any) of each value in the structure. It will receive each key and value as its argument. This is bound to the object that contains the key. The value it returns is string-formatted.
Values not represented by JSON (such as functions and undefined) will be excluded.
The infinitely large number will be replaced with NULL, and you can replace the other value with a similar replacer function:

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

A suitable reviver function is provided for Json.parse to be implemented.

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), by contrast, appears to be much simpler. Nonetheless, a good HTML reference book is one inch thick. This is because the formatting and structuring of documents is a complex matter.
The advantage of most XML is that it is a role around 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 these make her independent of the application at a higher level than other data interchange formats. In fact, XML is already a standard of the way it is, which means there is no need to argue (or look at it) anymore.
Unfortunately, XML does not work well for data exchange, as a wrench is not used to nail nails. It contains too many wrappers, and it does not match the data model of most programming languages. Most programmers 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's another mosquito symbol, and she has all the benefits of XML, and she's better suited for data exchange. This symbol is the JavaScript object symbol (JSON).
The most sensible insight about XML (for example: xmlsuck.org) indicates that XML has a big 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 no other deficiencies.
Let's compare XML and JSON on 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 likely to write. At the same time, it is easier to read and write to 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 contain more information.
The same is true for JSON.
8) XML is easy to handle because the structure of the data is simple and standard.
JSON can be easier to handle because her structure is simpler.
9) There are many reusable software available to programmers 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. No extra software is needed at all. In other languages, only a small amount of JSON-specific code is required. For example, Json.org provides a package that contains 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 (which can be nested), 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 any display capability because she is not a document markup language.
13) Self-describing data
XML and JSON are the same at this point.
14) Full integration of all traditional databases and formats
(Statements about XML are sometimes a bit exaggerated) An XML document can contain any conceivable data type-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 an executable program in a data exchange system may introduce dangerous security issues.
15) Internationalization
Both XML and JSON are encoded in Unicode.
16) Openness and scalability
XML has an open structure that allows 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 certain industry-specific vocabularies.
These words can be automatically converted to JSON, which is very simple to migrate from XML to JSON.
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 more easily mapped 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 nature of the transition from XML to JSON make JSON more widely available.

JSON learning one (basic concept knowledge learning)

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.