jQuery 如何编写嵌套的多维json对象

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

How to write a nested multi dimensional json object

jqueryjson

提问by Gandalf

I am studying json and i was wondering if this is the right way to write a multi dimensional json object that is nested.I wrote:

我正在研究 json,我想知道这是否是编写嵌套的多维 json 对象的正确方法。我写道:

var foo = {
    "logged_in":true,
    "town":"Dublin",
    "state":"Ohio",
    "country":"USA",
    "products":2,
    "0":{
        "pic_id":"1500",
        "description":"Picture of a computer",
        "localion":"img.cloudimages.us/2012/06/02/computer.jpg",
        "type":"jpg",
        "childrenimages":2

        "0":{
        "pic_id":"15011",
        "description":"Picture of a cpu",
        "localion":"img.cloudimages.us/2012/06/02/mycpu.png",
        "type":"png"
          }
        "1":{
        "pic_id":"15012",
        "description":"Picture of a cpu two",
        "localion":"img.cloudimages.us/2012/06/02/thiscpu.png",
        "type":"png"
          }
    },
    "1":{
        "pic_id":"1501",
        "description":"Picture of a cpu",
        "localion":"img.cloudimages.us/2012/06/02/cpu.png",
        "type":"png"
    }
};

Is this right or is there a convention i should follow if my object becomes too deeply nested.

如果我的对象嵌套太深,这是正确的还是有我应该遵循的约定。

回答by Dmitry Kudryavtsev

Consider using arrays instead of numerated object.

考虑使用数组而不是数字对象。

Arrays in json are defined using [] http://www.json.org/

json 中的数组使用 [] http://www.json.org/定义

Here is an example:

下面是一个例子:

var foo = {
    "logged_in":true,
    "town":"Dublin",
    "state":"Ohio",
    "country":"USA",
    "products":
    [
        {
            "pic_id":"1500",
            "description":"Picture of a computer",
            "localion":"img.cloudimages.us/2012/06/02/computer.jpg",
            "type":"jpg",
            "childrenimages":
            [
                {
                    "pic_id":"15011",
                    "description":"Picture of a cpu",
                    "localion":"img.cloudimages.us/2012/06/02/mycpu.png",
                    "type":"png"
                },
                {
                    "pic_id":"15012",
                    "description":"Picture of a cpu two",
                    "localion":"img.cloudimages.us/2012/06/02/thiscpu.png",
                    "type":"png"
                }
            ]
        },
        {
            "pic_id":"1501",
            "description":"Picture of a cpu",
            "localion":"img.cloudimages.us/2012/06/02/cpu.png",
            "type":"png"
        }
    ],
};

(Forgive me if I forgot either closing { or [ or , its pretty hard to type code in SO :p )

(请原谅我,如果我忘记关闭 { 或 [ 或 ,在 SO 中输入代码非常困难 :p )

This way you dont even need to have counts like

这样你甚至不需要像

"products":2,

or

或者

"childrenimages":2

You simply do

你只是做

foo.products.length

or

或者

foo.products[0].childrenimages.length

Good luck :)

祝你好运 :)

回答by Tallboy

Here is the proper format for your data (note i changed some data itself)

这是您的数据的正确格式(注意我自己更改了一些数据)

{
    "logged_in":true,
    "town":"Dublin",
    "state":"Ohio",
    "country":"USA",
    "products":2,
    "productinfo":[
        {
            "0":{
                "pic_id":"1500",
                "description":"Picture of a computer",
                "localion":"img.cloudimages.us/2012/06/02/computer.jpg",
                "type":"jpg",
                "childrenimages":2
            },
            "1":{
                "pic_id":"15011",
                "description":"Picture of a cpu",
                "localion":"img.cloudimages.us/2012/06/02/mycpu.png",
                "type":"png"
            },
            "2":{
                "pic_id":"15012",
                "description":"Picture of a cpu two",
                "localion":"img.cloudimages.us/2012/06/02/thiscpu.png",
                "type":"png"
            },
            "3":{
                "pic_id":"1501",
                "description":"Picture of a cpu",
                "localion":"img.cloudimages.us/2012/06/02/cpu.png",
                "type":"png"
            }
        }
    ]
}

回答by jeschafe

That's not actually an array that you have there, that's just an object containing properties that are also objects. You're also missing a few commas so this won't even compile.

这实际上不是您在那里拥有的数组,它只是一个包含也是对象的属性的对象。您还缺少一些逗号,因此它甚至无法编译。

What might be more convenient for you in this case is to use both arrays and objects to form your JSON. For Example:

在这种情况下,对您来说更方便的是同时使用数组和对象来形成您的 JSON。例如:

 var this_json_string = {
    "state":"Ohio",
    "country":"USA",
    "products":[
        {
          "pic_id":"1500",
          "description":"Picture of a computer",
        },

        {
          "pic_id":"15011",
          "description":"Picture of a cpu"
        },
        {
          "pic_id":"15012",
          "description":"Picture of a cpu two"
        },
        {
          "pic_id":"1501",
          "description":"Picture of a cpu"
        }
    ]
};

回答by Florian Margaine

Don't write JSON. Seriously, except for simple configuration files, don't write JSON.

不要写JSON。说真的,除了简单的配置文件,不要写JSON。

You have utilities to convert objects to a JSON string in most languages (if not any).

您可以使用大多数语言(如果没有)将对象转换为 JSON 字符串的实用程序。

PHP: json_encode($array);

PHP: json_encode($array);

Javascript: JSON.stringify( obj );

Javascript: JSON.stringify( obj );

Etc.

等等。

Writing JSON manually often leads to syntax errors. The kind that gives you headaches until you see that missing comma or w/e. You have good tools to do this, use them. You could compare to XML, but JSON has no tool (IDEs, text editor) saying "This syntax is wrong" while you're typing it. For example, no editor will tell you that you used a single quote instead of a double.

手动编写 JSON 通常会导致语法错误。那种让您头疼的类型,直到您看到缺少的逗号或 w/e。你有很好的工具来做到这一点,使用它们。您可以与 XML 进行比较,但 JSON 没有工具(IDE、文本编辑器)在您键入时提示“此语法错误”。例如,没有编辑会告诉您您使用了单引号而不是双引号。

Just don't write JSON.

只是不要写JSON。