JSON in JavaScript
2008-10-10 21:27
302 查看
JavaScript is a general
purpose programming language that was introduced as the page scripting
language for Netscape Navigator. It
is still widely believed to be a subset of Java, but it is not. It
is a Scheme-like
language with C-like
syntax and soft
objects. JavaScript was standardized in the ECMAScript
Language Specification, Third Edition.
JSON is a subset of the object literal notation
of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language
with no muss or fuss.
In this example, an object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members.
Members can be retrieved using dot or subscript operators.
To convert a JSON text into an object, you can use the eval()
function. eval() invokes the JavaScript compiler. Since JSON
is a proper subset of JavaScript, the compiler will correctly parse the
text and produce an object structure. The text must be wrapped in parens
to avoid tripping on an ambiguity in JavaScript's syntax.
The
and execute any JavaScript program, so there can be security issues. The
use of
It is much safer to use a JSON parser. In web applications over XMLHttpRequest,
communication is permitted only to the same origin that provide that page,
so it is trusted. But it might not be competent. If the server is not
rigorous in its JSON encoding, or if it does not scrupulously validate
all of its inputs, then it could deliver invalid JSON text that could
be carrying dangerous script. The
the script, unleashing its malice.
To defend against this, a JSON parser should be used. A JSON parser will
recognize only JSON text, rejecting all scripts. In browsers that provide
native JSON support, JSON parsers are also much faster than
It is expected that native JSON support will be included in the next ECMAScript
standard.
The optional
called for every key and value at every level of the final result. Each
value will be replaced by the result of the
This can be used to reform generic objects into instances of pseudoclasses,
or to transform date strings into
A JSON stringifier goes in the opposite direction, converting JavaScript
data structures into JSON text. JSON does not support cyclic data structures,
so be careful to not give cyclical structures to the JSON stringifier.
If the
method, it calls that method, and stringifies the value returned. This
allows an object to determine its own JSON representation.
The
These strings are used to select the properties that will be included
in the JSON text.
The
It will be called after the
on each of the values in the structure. It will be passed each key and
value as parameters, and
the key. The value returned will be stringified.
If you do not provide an array or replacer function, then an optional
replacer function will be provided for you that omits inherited properties.
If you want all inherited properties, you can provide a simple replacer
function:
Values that do not have a representation in JSON (such as functions and
Nonfinite numbers are replaced with
other values, you could use a replacer function like this:
purpose programming language that was introduced as the page scripting
language for Netscape Navigator. It
is still widely believed to be a subset of Java, but it is not. It
is a Scheme-like
language with C-like
syntax and soft
objects. JavaScript was standardized in the ECMAScript
Language Specification, Third Edition.
JSON is a subset of the object literal notation
of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language
with no muss or fuss.
var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] };
In this example, an object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members.
Members can be retrieved using dot or subscript operators.
myJSONObject.bindings[0].method // "newURI"
To convert a JSON text into an object, you can use the eval()
function. eval() invokes the JavaScript compiler. Since JSON
is a proper subset of JavaScript, the compiler will correctly parse the
text and produce an object structure. The text must be wrapped in parens
to avoid tripping on an ambiguity in JavaScript's syntax.
var myObject = eval('(' + myJSONtext + ')');
The
evalfunction is very fast. However, it can compile
and execute any JavaScript program, so there can be security issues. The
use of
evalis indicated when the source is trusted and competent.
It is much safer to use a JSON parser. In web applications over XMLHttpRequest,
communication is permitted only to the same origin that provide that page,
so it is trusted. But it might not be competent. If the server is not
rigorous in its JSON encoding, or if it does not scrupulously validate
all of its inputs, then it could deliver invalid JSON text that could
be carrying dangerous script. The
evalfunction would execute
the script, unleashing its malice.
To defend against this, a JSON parser should be used. A JSON parser will
recognize only JSON text, rejecting all scripts. In browsers that provide
native JSON support, JSON parsers are also much faster than
eval.
It is expected that native JSON support will be included in the next ECMAScript
standard.
var myObject = JSON.parse(myJSONtext, reviver);
The optional
reviverparameter is a function that will be
called for every key and value at every level of the final result. Each
value will be replaced by the result of the
reviverfunction.
This can be used to reform generic objects into instances of pseudoclasses,
or to transform date strings into
Dateobjects.
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; });
A JSON stringifier goes in the opposite direction, converting JavaScript
data structures into JSON text. JSON does not support cyclic data structures,
so be careful to not give cyclical structures to the JSON stringifier.
var myJSONText = JSON.stringify(myObject, replacer);
If the
stringifymethod sees an object that contains a
toJSON
method, it calls that method, and stringifies the value returned. This
allows an object to determine its own JSON representation.
The
stringifiermethod can take an optional array of strings.
These strings are used to select the properties that will be included
in the JSON text.
The
stringifiermethod can take an optional replacer function.
It will be called after the
toJSONmethod (if there is one)
on each of the values in the structure. It will be passed each key and
value as parameters, and
thiswill be bound to object holding
the key. The value returned will be stringified.
If you do not provide an array or replacer function, then an optional
replacer function will be provided for you that omits inherited properties.
If you want all inherited properties, you can provide a simple replacer
function:
var myJSONText = JSON.stringify(myObject, function (key, value) { return value; });
Values that do not have a representation in JSON (such as functions and
undefined) are excluded.
Nonfinite numbers are replaced with
null. To substitute
other values, you could use a replacer function like this:
function replacer(key, value) { if (typeof value === 'number' && !isFinite(value)) { return String(value); } return value; } The open source code of a JSON parser and JSON stringifier is available. When minified it is less than 2.5K.
相关文章推荐
- Convert objects to JSON in C# using JavaScriptSerializer
- JSON 学习之JSON in JavaScript详细使用说明
- [Javascript] Classify JSON text data with machine learning in Natural
- Convert objects to JSON in C# using JavaScriptSerializer
- directly receive json data from javascript in mvc
- 主题:JSON详细学习之JSON in JavaScript
- JSON详细学习之JSON in JavaScript
- Convert objects to JSON in C# using JavaScriptSerializer
- JSON 学习之JSON in JavaScript详细使用说明
- \t\t主题:JSON详细学习之JSON in JavaScript
- JSON详细学习之JSON in JavaScript
- json in javascript
- javascript for-in有序遍历json数据并探讨各个浏览器差异
- Cross-browser JSON Serialization in JavaScript
- json to obj and obj to json in javascript
- DataTable JSON Serialization in JSON.NET and JavaScriptSerializer
- JSON in JavaScript
- [转] json in javascript
- JSON in JavaScript Asp.net
- An Introduction to JavaScript Object Notation (JSON) in JavaScript and .NET