Chinese Translation of JSON in JavaScript _ json

Source: Internet
Author: User
Tags tojson
JSON is a subset of JavaScript object-oriented syntax. JSON is a subset of JavaScript, so it can be clearly used in this language. Let's take a look at the following example.

Script var json_jb51 = {"showinfo": [{"title": "", "url": "www.jb51.net", "author": "dxy "}, {"title": "server", "url": "s.jb51.net", "author": "dxy" },{ "title": "software download", "url ": "www.jb51.net/softs", "author": "dxy"}]}; alert ("our website name:" + json_jb51.showinfo [0]. title); alert ("our Website:" + json_jb51.showinfo [0]. url); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


The primary purpose of JavaScript programming language is to provide a Page scripting language for Netscape Navigator. It is still widely regarded as a subset of Java, but this is not the case. It is a Scheme-like language with a syntax similar to the C language and supports object-oriented programming. JavaScript uses the third edition of the ECMAScript language specification for standardization.

JSON is a subset of JavaScript object-oriented syntax. JSON is a subset of JavaScript, so it can be clearly used in this language.

The Code is as follows:


Var myJSONObject = {"bindings ":[
{"IrcEvent": "PRIVMSG", "method": "newURI", "regex": "^ http ://.*"},
{"IrcEvent": "PRIVMSG", "method": "deleteURI", "regex": "^ delete .*"},
{"IrcEvent": "PRIVMSG", "method": "randomURI", "regex": "^ random .*"}
]
};


In the preceding example, an object with a separate member "bindings" is created. This member includes an array containing three objects ("ircEvent", "method", and "regex ").
Members can be retrieved through the. Or subscript operator.

The Code is as follows:


MyJSONObject. bindings [0]. method // "newURI"


To convert JSON text to an object, you can use the eval () function. The eval () function calls the JavaScript editor. Since JSON is a subset of JavaScript, the compiler will parse the text correctly and generate the object structure. The text must be enclosed in brackets to avoid syntax ambiguity in JavaScript.

The Code is as follows:


Var myObject = eval ('+ myJSONtext + ')');


The eval function is very fast. It can compile and execute any JavaScript program, resulting in security issues. The eval function can be used only when trusted and complete source code is used. This makes it safer to use the JSON parser. For web applications that use XMLHttpRequest, communication between pages only allows the same source, so it can be trusted. However, this is not perfect. If the server does not have strict JSON encoding or strict input verification, invalid JSON text including dangerous Scripts may be transmitted. The eval function executes malicious scripts.
The JSON parser can prevent such events. The JSON parser can only recognize JSON text and reject all scripts. The browser's JSON parser that provides local JSON support will be much faster than the eval function. The ECMAScript standard is expected to support local JSON in the future.

The Code is as follows:


Var myObject = JSON. parse (myJSONtext, reviver );


A replacement function is called as an optional parameter by the key and value at each level of the final result. Each value is replaced by the value of the replacement function. This can be used to change a common class to a pseudo-class instance, or to convert a date string to a date object.

The Code is as follows:


MyData = JSON. parse (text, function (key, value ){
Var type;
If (value & typeof value = 'object '){
Type = value. type;
If (typeof type = 'string' & typeof window [type] = 'function '){
Return new (window [type]) (value );
}
}
Return value;
});


JSON stringifier performs reverse operations to convert the JavaScript data structure to JSON text. JSON does not support the cyclic data structure, so be careful not to provide the cyclic structure for JSON stringifier.

The Code is as follows:


Var myJSONText = JSON. stringify (myObject, replacer );


If the stringify function finds an object with the toJSON method, it executes this method and returns the generated value. Such an object can determine its own JSON representation.
The stringifier method can carry an optional string array. These strings are used to select attributes included in JSON text.
The stringifier method can carry an optional replacer function. It will be executed after the toJSON method (if any) of each value in the structure. It transmits each key and value as a parameter. Of course, the object must contain this key. The return value will be stringized.
If no array or substitution function is provided, an optional substitution function is provided to ignore the integrated attributes. If you want all the inherited attributes, you can provide a simple replacement function:

The Code is as follows:


Var myJSONText = JSON. stringify (myObject, function (key, value ){
Return value;
});


Values that are not expressed in JSON (such as functions and undefined) are excluded.
Uncertain quantity will be replaced with null. To replace other values, you can use the replacer function as follows:

The Code is as follows:


Function replacer (key, value ){
If (typeof value = 'number '&&! IsFinite (value )){
Return String (value );
}
Return value;
}


The open-source JSON parser and JSON stringifier can be used. Minified can be smaller than 2.5 kb.

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.