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

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

问题描述

在 javascript 中创建对象的正确语法是什么,它可以在大多数网络浏览器中工作(我的意思是: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 是正确:JSON 规范 规定使用引号以使其实际上是 JSON.如果不使用引号,它可能是 Javascript 中的有效对象文字,但它不是 JSON.除了浏览器端 Javascript 之外的其他服务使用 JSON(例如使用 php、Java 等的 web 服务),如果您构造一个缺少引号的字符串,则不能保证它会被正确解析——尽管我怀疑大多数实现都是足够强大,可以这样做.

@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.

仅供参考,在 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天全站免登陆