Javascript 嵌套的 JSON 对象 - 我是否必须对所有内容都使用数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2098276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:45:23  来源:igfitidea点击:

Nested JSON objects - do I have to use arrays for everything?

javascriptjqueryjsonsyntaxparsing

提问by John Schulze

Is there any way to have nested objects in JSON so I don't have to make arrays out of everything? For my object to be parsed without error I seem to need a structure like this:

有什么方法可以在 JSON 中嵌套对象,这样我就不必从所有内容中创建数组?为了正确解析我的对象,我似乎需要这样的结构:

{"data":[{"stuff":[
    {"onetype":[
        {"id":1,"name":"John Doe"},
        {"id":2,"name":"Don Joeh"}
    ]},
    {"othertype":[
        {"id":2,"company":"ACME"}
    ]}]
},{"otherstuff":[
    {"thing":
        [[1,42],[2,2]]
    }]
}]}

If I fetch this object into a variable called "result" I have to access the nested objects like this:

如果我将此对象提取到名为“result”的变量中,则必须像这样访问嵌套对象:

result.data[0].stuff[0].onetype[0]

and

result.data[1].otherstuff[0].thing[0]

This seems clumsy and redundant to me, if possible I would prefer:

这对我来说似乎很笨拙和多余,如果可能的话,我更愿意:

result.stuff.onetype[0]

and

result.otherstuff.thing

But how can I use the object keys directly when everything is an array? To my confused and uneducated mind something like this would seem more appropriate:

但是当一切都是数组时,如何直接使用对象键?在我困惑和未受过教育的头脑中,这样的事情似乎更合适:

{"data":
    {"stuff":
        {"onetype":[
            {"id":1,"name": ""},
            {"id":2,"name": ""}
        ]}
        {"othertype":[
            {"id":2,"xyz": [-2,0,2],"n":"Crab Nebula","t":0,"c":0,"d":5}
        ]}
    }
    {"otherstuff":
        {"thing":
            [[1,42],[2,2]]
        }
    }
}

I've probably misunderstood something fundamental here, but I cannot get the jQuery parser (nor the native FF parser used by jQuery 1.4) to accept the second style object. If anyone can enlighten me it would be gratefully appreciated!

我可能在这里误解了一些基本的东西,但我无法让 jQuery 解析器(也不是 jQuery 1.4 使用的本机 FF 解析器)接受第二个样式对象。如果有人能启发我,将不胜感激!

回答by SLaks

You don't need to use arrays.

您不需要使用数组。

JSON values can be arrays, objects, or primitives (numbers or strings).

JSON 值可以是数组、对象或基元(数字或字符串)。

You can write JSON like this:

您可以像这样编写 JSON:

{ 
    "stuff": {
        "onetype": [
            {"id":1,"name":"John Doe"},
            {"id":2,"name":"Don Joeh"}
        ],
        "othertype": {"id":2,"company":"ACME"}
    }, 
    "otherstuff": {
        "thing": [[1,42],[2,2]]
     }
}

You can use it like this:

你可以这样使用它:

obj.stuff.onetype[0].id
obj.stuff.othertype.id
obj.otherstuff.thing[0][1]  //thing is a nested array or a 2-by-2 matrix.
                            //I'm not sure whether you intended to do that.

回答by Igor Zevaka

Every object has to be named inside the parent object:

每个对象都必须在父对象内命名:

{ "data": {
    "stuff": {
        "onetype": [
            { "id": 1, "name": "" },
            { "id": 2, "name": "" }
        ],
        "othertype": [
            { "id": 2, "xyz": [-2, 0, 2], "n": "Crab Nebula", "t": 0, "c": 0, "d": 5 }
        ]
    },
    "otherstuff": {
        "thing":
            [[1, 42], [2, 2]]
    }
}
}

So you cant declare an object like this:

所以你不能像这样声明一个对象:

var obj = {property1, property2};

It has to be

它一定要是

var obj = {property1: 'value', property2: 'value'};

回答by syarul

You have too many redundant nested arrays inside your jSON data, but it is possible to retrieve the information. Though like others have said you might want to clean it up.

您的 JSON 数据中有太多冗余嵌套数组,但可以检索这些信息。尽管就像其他人所说的那样,您可能想要清理它。

use each() wrap within another each() until the last array.

使用 each() 包裹在另一个 each() 中,直到最后一个数组。

for result.data[0].stuff[0].onetype[0]in jQueryyou could do the following:

因为result.data[0].stuff[0].onetype[0]jQuery 中,您可以执行以下操作:

`

`

$.each(data.result.data, function(index0, v) {
    $.each(v, function (index1, w) {
        $.each(w, function (index2, x) {
            alert(x.id);
        });
    });

});

`

`