JSON in JavaScript Chinese translation _json

Source: Internet
Author: User
Tags data structures eval tojson
You can look at the following examples first
<script> var json_jb51 = {"Showinfo": [{"title": "Cloud-dwelling community", "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 ("Name of our site:" +json_jb51.showinfo[0].title); Alert ("Our website URL:" +json_jb51.showinfo[0].url); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The primary purpose of JavaScript, the programming language, is to provide a page scripting language for Netscape Navigator. It is still generally considered a subset of Java, but that is not the case. It is a syntax-like C language and supports an object-oriented scheme-like language. JavaScript is standardized using the third edition of the ECMAScript Language specification.

JSON is a subset of JavaScript object-oriented syntax. Because JSON is a subset of JavaScript, it can be used clearly in this language.
Copy Code code as follows:

var myjsonobject = {"Bindings": [
{"Ircevent": "Privmsg", "Method": "Newuri", "regex": "^http://.*"},
{"Ircevent": "Privmsg", "Method": "Deleteuri", "regex": "^delete.*"},
{"Ircevent": "Privmsg", "Method": "Randomuri", "regex": "^random.*"}
]
};

The above example creates an object that includes a separate member, "bindings," that includes an array of three objects ("Ircevent", "Method", and "regex")
Members can be retrieved by the. or subscript operator.
Copy Code code as follows:

Myjsonobject.bindings[0].method//"Newuri"

To convert JSON text to objects, you can use the eval () function. The eval () function calls the JavaScript editor. Because JSON is a subset of JavaScript, the compiler will parse the text correctly and produce an object structure. Text must be enclosed in parentheses to avoid grammatical ambiguity in JavaScript.
Copy Code code as follows:

var myObject = eval (' (' + myjsontext + ') ');

The Eval function is very fast. It can compile and execute any JavaScript program, thus creating a security issue. You can use the Eval function when you are using trusted and sophisticated source code. This makes it more secure to use the JSON parser. With XMLHttpRequest Web applications, communication between pages only allows homology and therefore can be trusted. But this is not perfect. If the server does not have a rigorous JSON encoding, or if there is no strict input validation, then it is possible to route invalid JSON literals that include dangerous scripts. The Eval function will execute a malicious script.
Use the JSON parser to prevent such events. The JSON parser can only recognize JSON text and reject all scripts. The JSON parser for browsers that provide local JSON support will be much faster than the Eval function. The future ECMAScript standard is expected to support local JSON.
Copy Code code as follows:

var myObject = Json.parse (Myjsontext, reviver);

A substitution function (Reviver function) is invoked as an optional parameter by the key (key) and value (value) of the final result. Each value is replaced by the value of the substituted function. This can be used to change a generic class to an instance of a pseudo class, or to convert a date string to a Date object.
Copy Code code 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;
});

The JSON stringifier is reversed to convert the JavaScript data structure to JSON text. JSON does not support looping data structures, so be careful not to provide a looping structure for JSON stringifier.
Copy Code code as follows:

var myjsontext = json.stringify (MyObject, replacer);

If the Stringify function finds an object with a Tojson method, it executes this method and returns the resulting value. Such an object can determine its own JSON performance.
The Stringifier method can carry an optional array of strings. These strings are used to select the attributes that are included in the JSON text.
The Stringifier method can carry an optional substitution (replacer) function. It will be executed after the Tojson method (if any) for each value in the structure. It passes each key and value as a parameter, and of course the object contains the key. The return value will be serialized.
If an array or substitution function is not provided, an optional substitution function for ignoring the integrated attribute will be provided. If you want all inherited properties, you can provide a simple substitution function:
Copy Code code as follows:

var myjsontext = json.stringify (myObject, function (key, value) {
return value;
});

The values that are not expressed in JSON, such as functions and undefined, are excluded.
The quantity that is not determined will be replaced with NULL. To replace other values, you can use the substitution (replacer) function as follows
Copy Code code 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. Through minified can be less than 2.5K.

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.