Javascript JSON 对象 .. 在 JSON 对象中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7031465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON objects .. inside JSON objects
提问by grep
so I am new to JSON, and have been experimenting around with some possibilities. One thing I was wondering: Is there a way to place 'JSON object's inside of 'JSON objects'?I want to assume this can be done, and would like it to be possible as it could be very useful, but all attempts at the syntax has failed me. Here is an example of the standard:
所以我是 JSON 的新手,并且一直在尝试一些可能性。我想知道的一件事是:有没有办法将“JSON 对象”放在“JSON 对象”中?我想假设这是可以完成的,并且希望它成为可能,因为它可能非常有用,但是对语法的所有尝试都失败了。以下是标准示例:
var Person = {
name: 'John',
age: 21,
alive: true,
siblings: [
{
name: 'Andrew',
age: 23,
alive: true
},
{
name: 'Christine',
age: 19,
alive: true
}
]
}
Now, is there a way to do something like the following?
现在,有没有办法做类似下面的事情?
var Andrew = {
name: 'Andrew',
age: 21,
alive: true
}
var Person = {
name: 'John',
age: 21,
alive: true,
siblings: [
{
Andrew
},
{
name: 'Christine',
age: 19,
alive: true
}
]
}
If so, what is the proper way to do this? Or can it simply, not be done?
如果是这样,这样做的正确方法是什么?或者可以简单地,不做?
edit: What I really mean is: Is JSON able to encode objects which have objects inside of them?
编辑:我真正的意思是:JSON 是否能够对内部包含对象的对象进行编码?
采纳答案by gilly3
Omit the curly braces:
省略花括号:
var Person = {
name: 'John',
age: 21,
alive: true,
siblings: [
Andrew,
{
name: 'Christine',
age: 19,
alive: true
}
]
}
Andrew
is a referenceto a JavaScript object. The curly brace notation - { foo: 1 }
- is an object literal. To use a variable instead of a literal, you omit the entire literal syntax, including the curly braces.
Andrew
是对 JavaScript 对象的引用。花括号符号 - { foo: 1 }
- 是一个对象字面量。要使用变量而不是文字,请省略整个文字语法,包括大括号。
Note that neither of these is JSON or a "JSON object". JSON is a string that happens to match JavaScript Object literal syntax. Once a JSON string has been parsed, it is a JavaScript object, not a JSON object.
请注意,这些都不是 JSON 或“JSON 对象”。JSON 是一个恰好匹配 JavaScript 对象文字语法的字符串。解析 JSON 字符串后,它就是一个 JavaScript 对象,而不是 JSON 对象。
For example, this is valid JavaScript, but not valid JSON:
例如,这是有效的 JavaScript,但不是有效的 JSON:
var Person = {
name: "John",
birthDate: new Date(1980, 0, 1),
speak: function(){ return "hello"; },
siblings: [
Andrew,
Christine
];
}
JSON cannot instantiate objects such as new Date()
, JSON cannot have a function as a member, and JSON cannot reference external objects such as Andrew
or Christine
.
JSON 不能实例化对象,例如new Date()
,JSON 不能有函数作为成员,JSON 不能引用外部对象,例如Andrew
或Christine
。
回答by Dave Ward
You're close. Andrew
is already an object, so you don't need to wrap it in the object literal syntax (and you'd need a property name to accompany it as the value if you did that). How about this:
你很接近。 Andrew
已经是一个对象,因此您不需要将它包装在对象文字语法中(如果这样做,您需要一个属性名称作为值伴随它)。这个怎么样:
var Andrew = {
name: 'Andrew',
age: 21,
alive: true
}
var Person = {
name: 'John',
age: 21,
alive: true,
siblings: [
Andrew,
{
name: 'Christine',
age: 19,
alive: true
}
]
}
回答by Joe
var Andrew = {
name: 'Andrew',
age: 21,
alive: true
}
var Person = {
name: 'John',
age: 21,
alive: true,
siblings: [
Andrew,
{
name: 'Christine',
age: 19,
alive: true
}
]
}
Drop the brackets since it is already an object.
去掉括号,因为它已经是一个对象。
Person.siblings[0].name // Andrew
回答by Amir Raminfar
You don't need brackets again. You can just put Andrew
as one of the array values.
您不再需要括号。您可以将其Andrew
作为数组值之一放置。
siblings: [
Andrew,
{
name: 'Christine',
age: 19,
alive: true
}
]
回答by Alex Turpin
In pure JSON, you can embed objects in objects allright, just not referencesof objects.
在纯 JSON 中,您可以将对象嵌入对象中,但不能嵌入对象的引用。
If you are using JSON in JavaScript, this is doable, albeit you don't need the brackets around it.
如果您在 JavaScript 中使用 JSON,这是可行的,尽管您不需要括号。