简体   繁体   English

JSON 对象.. 在 JSON 对象内

[英]JSON objects .. inside JSON objects

so I am new to JSON, and have been experimenting around with some possibilities.所以我是 JSON 的新手,并且一直在尝试一些可能性。 One thing I was wondering: Is there a way to place 'JSON object's inside of 'JSON objects'?我想知道的一件事:有没有办法将'JSON对象'放在'JSON对象'中? 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:下面是一个标准的例子:

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 是否能够编码其中包含对象的对象?

Omit the curly braces:省略花括号:

var Person = {
    name:  'John', 
    age:   21, 
    alive: true,
    siblings: [
        Andrew,
        {
            name:  'Christine',
            age:   19,
            alive: true
        }
    ]    
}

Andrew is a reference to a JavaScript object. Andrew是对 JavaScript object 的引用 The curly brace notation - { foo: 1 } - is an object literal .花括号符号 - { foo: 1 } - 是 object文字 To use a variable instead of a literal, you omit the entire literal syntax, including the curly braces.要使用变量而不是文字,您可以省略整个文字语法,包括花括号。

Note that neither of these is JSON or a "JSON object".请注意,这些都不是 JSON 或“JSON 对象”。 JSON is a string that happens to match JavaScript Object literal syntax. JSON 是恰好匹配 JavaScript Object 文字语法的字符串。 Once a JSON string has been parsed, it is a JavaScript object, not a JSON object. Once a JSON string has been parsed, it is a JavaScript object, not a JSON object.

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

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). Andrew已经是 object,因此您不需要将其包装在 object 文字语法中(如果您这样做,您需要一个属性名称作为值伴随它)。 How about this:这个怎么样:

var Andrew = {
  name:  'Andrew', 
  age:   21, 
  alive: true
}

var Person = {
  name:  'John', 
  age:   21, 
  alive: true,
  siblings: [
    Andrew,
    {
        name:  'Christine',
        age:   19,
        alive: true
    }
  ]    
}

You've got the right idea, look at this question, it's quite similar to yours.你的想法是对的,看看这个问题,它和你的很相似。 The accepted answer should be what you're looking for: Nested JSON objects - do I have to use arrays for everything?接受的答案应该是您正在寻找的: 嵌套的 JSON 对象 - 我必须使用 arrays 吗?

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.去掉括号,因为它已经是 object。

Person.siblings[0].name // Andrew

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
    }
] 

In pure JSON, you can embed objects in objects allright, just not references of objects.在纯 JSON 中,您可以将对象嵌入到对象中,但不能引用对象。

If you are using JSON in JavaScript, this is doable, albeit you don't need the brackets around it.如果您在 JavaScript 中使用 JSON,这是可行的,尽管您不需要它周围的括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM