0

我对这个 JSON 对象非常陌生。所以我需要你们的帮助。我必须创建一个 JSON 对象,并且该对象有两个孩子。一个孩子有几个孩子。如果我的问题令人困惑,请参考下图。它类似于嵌套列表。示例:

PRODUCT_LIST

<ul>
    <li>CATEGORY_ID - A</li>
    <li>PRODUCT_DETAILS
        <ul>
            <li>PRODUCT_ID - A.1</li>
            <li>PRODUCT_NAME - AAAA1111</li>
            <li>UNIT_COST - 0.1</li>
        </ul>
    </li>
    <li>CATEGORY_ID - B</li>
    <li>PRODUCT_DETAILS
        <ul>
            <li>PRODUCT_ID - B.1</li>
            <li>PRODUCT_NAME - BBBBB1111</li>
            <li>UNIT_COST - 0.2</li>
        </ul>
    </li>
</ul>

我尝试这样做,但遇到了 JavaScript 错误。任何人都可以帮助为上述图表创建 JSON。注意:一个类别有多个产品详细信息。这意味着 PRODUCT_ID、PRODUCT_NAME 和 UNIT_COST 将针对特定 CATEGORY_ID 重复多次。

我尝试过的 JavaScript 代码

var product =   '{"products_list":' +
                '[' + 
                    '{' + 
                        '{"category_id":"A"},' +
                        '{"product_details":' + 
                            '[' + 
                                '{"product_id":"A.1","product_name":"AAAA1111", "unit_cost":"A1"},' +
                                '{"product_id":"A.2","product_name":"AAAA2222", "unit_cost":"A2"},' +
                                '{"product_id":"A.3","product_name":"AAAA3333", "unit_cost":"A3"},' +
                                '{"product_id":"A.4","product_name":"AAAA4444", "unit_cost":"A4"},' +
                                '{"product_id":"A.5","product_name":"AAAA5555", "unit_cost":"A5"},' +
                                '{"product_id":"A.6","product_name":"AAAA6666", "unit_cost":"A6"},' +
                                '{"product_id":"A.7","product_name":"AAAA7777", "unit_cost":"A7"},' +
                                '{"product_id":"A.8","product_name":"AAAA8888", "unit_cost":"A8"},' +
                                '{"product_id":"A.9","product_name":"AAAA9999", "unit_cost":"A9"},' +
                                '{"product_id":"A.0","product_name":"AAAA0000", "unit_cost":"A0"}' +
                            ']' + 
                        '}' + 
                    '},' + 
                    '{' + 
                        '{"category_id":"A"},' +
                        '{"product_details":' + 
                            '[' + 
                                '{"product_id":"A.1","product_name":"AAAA1111", "unit_cost":"A1"},' +
                                '{"product_id":"A.2","product_name":"AAAA2222", "unit_cost":"A2"},' +
                                '{"product_id":"A.3","product_name":"AAAA3333", "unit_cost":"A3"},' +
                                '{"product_id":"A.4","product_name":"AAAA4444", "unit_cost":"A4"},' +
                                '{"product_id":"A.5","product_name":"AAAA5555", "unit_cost":"A5"},' +
                                '{"product_id":"A.6","product_name":"AAAA6666", "unit_cost":"A6"},' +
                                '{"product_id":"A.7","product_name":"AAAA7777", "unit_cost":"A7"},' +
                                '{"product_id":"A.8","product_name":"AAAA8888", "unit_cost":"A8"},' +
                                '{"product_id":"A.9","product_name":"AAAA9999", "unit_cost":"A9"},' +
                                '{"product_id":"A.0","product_name":"AAAA0000", "unit_cost":"A0"}' +
                            ']' + 
                        '}' + 
                    '}' + 
                ']' + 
            '}';
4

3 回答 3

3

使用 HTML 树,您将获得 PRODUCT_LIST 的以下 JSON 表示:

[
   {
      "CATEGORY_ID":"A",
      "PRODUCT_DETAILS":[
         {
            "PRODUCT_ID":"A.1",
            "PRODUCT_NAME":"AAAA1111",
            "UNIT_COST":0.1
         }
      ]
   },
   {
      "CATEGORY_ID":"B",
      "PRODUCT_DETAILS":[
         {
            "PRODUCT_ID":"B.1",
            "PRODUCT_NAME":"BBBBB1111",
            "UNIT_COST":0.2
         }
      ]
   }
]

但是,您的 JavaScript 会生成一个几乎有效的 JSON字符串,并将其分配给product变量。你可以用JSON.parse()它,但这太复杂了。由于JavaScriptObjectNotation是 Javascript对象文字语法的子集,您应该直接分配对象。

在您的 JSON 中,您在类别对象周围有太多大括号。您可以使用http://jsonformatter.curiousconcept.com/进行检查。更正的脚本:

var product = {
   "products_list":[
      {
         "category_id":"A",
         "product_details":[
            {
               "product_id":"A.1",
               "product_name":"AAAA1111",
               "unit_cost":"A1"
            },
            {
               "product_id":"A.2",
               "product_name":"AAAA2222",
               "unit_cost":"A2"
            },
            {
               "product_id":"A.3",
               "product_name":"AAAA3333",
               "unit_cost":"A3"
            },
            {
               "product_id":"A.4",
               "product_name":"AAAA4444",
               "unit_cost":"A4"
            },
            {
               "product_id":"A.5",
               "product_name":"AAAA5555",
               "unit_cost":"A5"
            },
            {
               "product_id":"A.6",
               "product_name":"AAAA6666",
               "unit_cost":"A6"
            },
            {
               "product_id":"A.7",
               "product_name":"AAAA7777",
               "unit_cost":"A7"
            },
            {
               "product_id":"A.8",
               "product_name":"AAAA8888",
               "unit_cost":"A8"
            },
            {
               "product_id":"A.9",
               "product_name":"AAAA9999",
               "unit_cost":"A9"
            },
            {
               "product_id":"A.0",
               "product_name":"AAAA0000",
               "unit_cost":"A0"
            }
         ]
      },
      {
         "category_id":"A",
         "product_details":[
            {
               "product_id":"A.1",
               "product_name":"AAAA1111",
               "unit_cost":"A1"
            },
            {
               "product_id":"A.2",
               "product_name":"AAAA2222",
               "unit_cost":"A2"
            },
            {
               "product_id":"A.3",
               "product_name":"AAAA3333",
               "unit_cost":"A3"
            },
            {
               "product_id":"A.4",
               "product_name":"AAAA4444",
               "unit_cost":"A4"
            },
            {
               "product_id":"A.5",
               "product_name":"AAAA5555",
               "unit_cost":"A5"
            },
            {
               "product_id":"A.6",
               "product_name":"AAAA6666",
               "unit_cost":"A6"
            },
            {
               "product_id":"A.7",
               "product_name":"AAAA7777",
               "unit_cost":"A7"
            },
            {
               "product_id":"A.8",
               "product_name":"AAAA8888",
               "unit_cost":"A8"
            },
            {
               "product_id":"A.9",
               "product_name":"AAAA9999",
               "unit_cost":"A9"
            },
            {
               "product_id":"A.0",
               "product_name":"AAAA0000",
               "unit_cost":"A0"
            }
         ]
      }
   ]
}
于 2012-08-22T10:39:07.330 回答
1

所有标识符都应该用引号引起来。
通过按类别 id 进行索引并制作对象的类别数组,我认为您可以获得数据结构的所有特性和功能,并使解决方案非常灵活。

var category = {
  "A": [
    { "product_id": "A.1", "product_name": "AAAA1111", "unit_cost": 0.1 },
    { "product_id": "A.2", "product_name": "AAAA2222", "unit_cost": 0.2 },
  ],
  "B": [
    { "product_id": "B.1", "product_name": "BBBB1111", "unit_cost": 0.2 },
    { "product_id": "B.2", "product_name": "BBBB2222", "unit_cost": 0.4 },
  ]
};
于 2012-08-22T10:42:18.140 回答
0

这是一个有点令人困惑的问题,但我可能会有类似这样的 JSON

var data = [
  {
    category_id: 'A',
    product_id: 'A.1',
    product_name: 'AAAA1111',
    unit_cost: 0.1
  },
  {
    category_id: 'B',
    product_id: 'B.1',
    product_name: 'BBBB1111',
    unit_cost: 0.2
  }
];

然后在显示时按类别过滤。您可能会发现http://underscorejs.org/对过滤结果很有用

于 2012-08-22T10:35:56.210 回答