Home >Web Front-end >JS Tutorial >JSON in JavaScript Chinese version translation_json

JSON in JavaScript Chinese version translation_json

WBOY
WBOYOriginal
2016-05-16 18:42:561739browse

You can first look at the following example


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

JavaScript is the primary programming language The purpose is to provide a page scripting language for Netscape Navigator. It is still commonly considered a subset of Java, but this is not the case. It is a Scheme-like language with syntax similar to C language and supports object-oriented programming. JavaScript is standardized using the third edition of the ECMAScript Language Specification.

JSON is a subset of JavaScript’s object-oriented syntax. Since JSON is a subset of JavaScript, 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.*"}
]
};


The above example creates an object containing a single member "bindings", which contains an object containing three objects ("ircEvent", "method", and "regex" ) array
members can be retrieved through the . or subscript operator. The code is as follows:


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


In order to convert JSON text into 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 correctly parse the text and produce object structures. Text must be enclosed in parentheses to avoid JavaScript syntax ambiguities. The code is as follows:


var myObject = eval('(' myJSONtext ')' );


The eval function is very fast. It can compile and execute any JavaScript program, thus creating security issues. The eval function should only be used when using trusted and complete source code. This makes it safer to use JSON parsers. For web applications that use XMLHttpRequest, communication between pages is only allowed from the same source, so it can be trusted. But it is not perfect. If the server does not have strict JSON encoding, or does not have strict input validation, it may transmit invalid JSON text including dangerous scripts. The eval function will execute the malicious script.
Using a JSON parser can prevent such incidents. The JSON parser can only recognize JSON text and reject all scripts. Browsers that provide native JSON support will have their JSON parsers much faster than the eval function. It is expected that future ECMAScript standards will support native JSON. The code is as follows:


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


A reviver function as an optional parameter is called for each level of key and value in the final result. Each value will be replaced by the value of the replacement function. This can be used to change a regular class into an instance of a pseudo-class, or to convert a date string into 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 operation and can convert JavaScript data structure into JSON text. JSON does not support cyclic data structures, so care should be taken not to provide cyclic structures to the JSON stringifier. The code is as follows:


var myJSONText = JSON.stringify(myObject, replacer);
<script> var json_jb51 = {"showinfo": [ {"title": "脚本之家", "url": "www.jb51.net", "author": "dxy"}, {"title": "服务器", "url": "s.jb51.net", "author": "dxy"}, {"title": "软件下载", "url": "www.jb51.net/softs", "author": "dxy"} ] }; alert("我们网站的名称:"+json_jb51.showinfo[0].title); alert("我们网站的网址:"+json_jb51.showinfo[0].url); </script>
If the stringify function finds an object with a toJSON method, it will execute this method and return the resulting 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 to include in the 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 passes each key and value as parameters, and of course the object contains the key. The return value will be stringified.
If no array or substitution function is provided, an optional substitution function for ignoring the integrated properties will be provided. If you want all inherited properties, you can provide a simple replacement function:
Copy code The code is as follows:

var myJSONText = JSON.stringify(myObject, function (key, value) {
return value;
});

For values ​​not expressed in JSON (such as functions and undefined) are excluded.
Undetermined quantities will be replaced with null. In order to replace other values, you can use the replacer function as follows
Copy code The code is as follows:

function replacer(key, value) {
if (typeof value === 'number' && !isFinite(value)) {
return String(value);
}
return value;
}

Open source JSON parser and JSON stringifier can use . Can be less than 2.5K via minified.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn