属性名称的JSON语法 [英] JSON syntax for property names

查看:111
本文介绍了属性名称的JSON语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中创建对象的正确语法是什么,它适用于大多数Web浏览器(我的意思是:IE 6 +,Firefox 2 +,Opera 9+)

What is the correct syntax to create objects in javascript that will work across the majority of web browsers (by that I mean : IE 6+, Firefox 2+, Opera 9+ )

这是否有效


var a={ "class": "Person", 
        "name": "William Shakespeare",
        "birthday": -12802392000000, 
        "nickname": "Bill" 
       } ;

或者是这样:


var a={ class: "Person", 
        name: "William Shakespeare",
        birthday: -12802392000000, 
        nickname: "Bill" 
       } ;

这两者有什么区别?

推荐答案

@AndreasN is正确 JSON规范规定使用引号以使其实际上是JSON。如果您不使用引号,它可能是Javascript中的有效对象文字,但它不是JSON。除浏览器端Javascript之外的其他服务使用JSON(例如使用php,Java等的webservices),如果构造一个缺少引号的字符串,则无法保证它将被正确解析 - 尽管我怀疑大多数实现都是足够健壮。

@AndreasN is correct: the JSON specification dictates the use of quotes in order for it to actually be JSON. If you don't use quotes, it may be a valid object literal in Javascript, but it is not JSON. Other services besides browser-side Javascript use JSON (e.g. webservices using php, Java, etc.) and if you construct a string that lacks the quotes, there is no guarantee that it will be parsed correctly -- although I suspect most implementations would be robust enough to do so.

FYI在Javascript中直接使用来自您无法防止恶意攻击的来源的JSON字符串的eval()是危险的。再次,请参阅 JSON网站,其中提供了更多解释以及非常简短的javascript文件它可以安全地将JSON字符串解析为Javascript对象。

FYI it is dangerous in Javascript to directly use eval() on JSON strings from sources which you cannot prevent malicious attacks. Again, see the JSON site which gives more of an explanation as well as a very short javascript file which safely parses JSON strings into Javascript objects.

编辑我觉得从技术上讲,你的原始问题不是关于JSON,而是Javascript的语法对象文字。不同之处在于,可从JSON字符串构造的对象将排除许多其他可能的对象文字,例如:

edit: I guess technically your original question is not about JSON but rather the Javascript's syntax for object literals. The difference is that objects constructable from a JSON string will exclude many other possible object literals, e.g.:

var a = {cat: "meow", dog: "woof"};
var aname = {cat: "Garfield", dog: "Odie"};
var b = {
  counter: 0,
  pow: function(x) { return x+1; },
  zap: function(y) { return (counter += y); }
};
var c = {
  all: [a,aname],
  animals: a,
  names: aname,
};

对象文字a和aname可以用JSON表示(通过向属性添加引号)名)。但是对象文字b和c不能。对象文字b包含函数(JSON中不允许)。上面的对象文字c包含对JSON中无法表示的方式的其他变量的引用,因为某些引用是共享的。如果您对 c.names 进行更改,它也会更改 c.all [1] ,因为它们共享参考到同一个变量。 JSON只能表示具有树结构的对象(例如,整个对象的每个子元素都是独立的)。

Object literals "a" and "aname" can be expressed in JSON (by adding quotes to the property names). But object literals "b" and "c" cannot. Object literal "b" contains functions (not allowed in JSON). Object literal "c" above contains references to other variables in a way that is not representable in JSON because some of the references are shared. If you make a change to c.names it will also change c.all[1] since they share a reference to the same variable. JSON can only express objects that have a tree structure (e.g. each sub-element of the overall object is independent).

这篇关于属性名称的JSON语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆