16

我正在研究 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"
    }
};

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

4

4 回答 4

23

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

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

这是一个例子:

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"
        }
    ],
};

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

这样你甚至不需要像

"products":2,

或者

"childrenimages":2

你只是做

foo.products.length

或者

foo.products[0].childrenimages.length

祝你好运 :)

于 2012-06-05T15:37:02.183 回答
2

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

在这种情况下,对您来说更方便的是同时使用数组和对象来形成 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"
        }
    ]
};
于 2012-06-05T16:37:14.443 回答
1

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

{
    "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"
            }
        }
    ]
}
于 2012-06-05T15:43:22.520 回答
-4

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

在大多数语言(如果没有的话)中,您都有将对象转换为 JSON 字符串的实用程序。

PHP:json_encode($array);

Javascript:JSON.stringify( obj );

等等。

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

只是不要写 JSON。

于 2012-06-05T15:37:19.530 回答