【问题标题】:How to write a nested multi dimensional json object如何编写嵌套的多维 json 对象
【发布时间】:2012-06-05 15:28:55
【问题描述】:

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

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

【问题讨论】:

  • 您正在将一个 Object 值分配给一个变量,该变量的名称暗示它包含一个字符串值...
  • 如果您的意思是“0”和“1”来表示数组中的对象,您将使用数组表示法[],并且不要指定“0”或“1”。 gist.github.com/2875889 之类的东西注意,你写的东西有很多小问题,这强调了下面的观点......不要写 JSON。

标签: jquery json


【解决方案1】:

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

在这种情况下,对您来说更方便的是同时使用数组和对象来形成您的 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"
        }
    ]
};

【讨论】:

    【解决方案2】:

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

    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
    

    祝你好运:)

    【讨论】:

    • 这实际上是一个理智的建议,使用数字索引作为对象属性名称是没有意义的。
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

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

      PHP:json_encode($array);

      Javascript:JSON.stringify( obj );

      等等

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

      不要写 JSON。

      【讨论】:

      • 你有什么反对写 JSON 的声明?我认为你的回答毫无意义。
      • 因为手动编写 JSON 经常会导致错误,如果您使用大多数语言提供的方法,则不会发生这种错误。既然可以避免这种错误,为什么还要冒这种风险呢?
      • @skwee 你确定吗?他建议您在语言中创建一个本机对象,然后将 json 字符串化:P 所以您实际上正在以不会失败和错误的方式编写 JSON
      • XML 从来都不是手写的。如果您知道正确的语言,您可以手动编写数据库,但那将是荒谬的。 JSON 也是如此。
      • @JamesP.Wright 除了 JSON 没有相同的工具来编写而不会出现语法错误(IDE 等)。例如,使用单引号而不是双引号不会在任何地方告诉您“这是错误的”。
      猜你喜欢
      • 2011-08-23
      • 2019-05-30
      • 2017-08-29
      • 2021-04-19
      • 2018-09-24
      • 2013-04-07
      • 2015-08-23
      • 2013-03-15
      • 2017-04-27
      相关资源
      最近更新 更多