论坛首页 Web前端技术论坛

JSON in JavaScript(翻译)

浏览 3121 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-05-07  
JavaScript is a general purpose programming language that was introduced as the page scripting language for Netscape Navigator. It is 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.

在这个例子中,一个对象被创建,它有一个"bindings"成员,"bindings"包含了一个数组,这个数组有3个对象,每个对象包含"ircEvent", "method","regex"这3个成员。
成员能通过点,或者下标访问。

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

To convert a JSON text into an object, 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.

转换一个JSON文本为对象,通常使用eval() 函数。eval() 运行在
JavaScript 编译器上。由于JSON是JavaScript的专属子集,编译器将会正确的解析文本,并且构造一个对象。

var myObject = eval('(' + myJSONtext + ')');
引用

The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. This is commonly the case in web applications when a web server is providing both the base page and the JSON data. There are cases where the source is not trusted. In particular, clients should never be trusted.

eval 函数运行非常快。但是,因为他能编译和执行任何JavaScript 程序,所以就存在一些安全问题。如果JavaScript 代码是安全可靠的则可以使用eval 函数。通常情况下WEB应用服务器上运行的程序是既提供基本页面也提供JSON数据。很多情况下这种代码是不可靠的。在某些情况下,客服端是根本不可靠的。

引用

When security is a concern it is better to use a JSON parser. A JSON parser will recognize only JSON text and so is much safer:

当安全性成为使用JSON 解析的一个障碍时,只解析JSON 文本则安全的多:

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

引用

The optional filter parameter 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 filter function. This can be used to reform generic objects into instances of classes, or to transform date strings into Date objects.

可选参数“filter”是一个函数,它会作用于最后结果里面的每一层上的键和值。每一个值将会被filter 函数过滤后重构。这一特性能把普通对象转换为类的实例,或者把日期字符串转化为日期对象。
    myData = JSON.parse(text, function (key, value) {        return key.indexOf('date') >= 0 ? new Date(value) : 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.

stringifier :将对象转换为字符文本。
JSON stringifier是反向的将一个JavaScript结果转化为JSON文本。JSON不支持循环数据结构,因此小心不要把一个循环结构用JSON stringifier处理。
var myJSONText = JSON.stringify(myObject);
引用

If the stringify method sees an object that contains a toJSON method, it calls the method, and stringifies the value returned. This allows an object to determine its own JSON representation.

如果一个stringifier方法发现一个对象包含有toJSON 方法,它将会调用toJSON 方法,返回字符串化后的值。这就是允许一个对象中断它自己的JSON表述。

引用

The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text. Otherwise, all of the properties of the object will be included. In any case, values that do not have a representation in JSON (such as functions and undefined) are excluded.

stringifier 方法能带一个字符串数组做参数。这些字符串参数被用来筛选将被包含在JSON 文本中的对象的属性。否则,对象所有的属性都将包含在JSON 文本中。在任何情况下,不包含JSON 表达式的值(例如functions、undefined)是被排除的。

The open source code of a JSON parser and JSON stringifier is available. When minified it is less than 2K.
  • json-javascript.rar (7.9 KB)
  • 描述: 附件:来自http://www.json.org/js.html 处理json的js库文件
  • 下载次数: 148
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics