我对这个JSON对象非常陌生。所以我需要你们的一些帮助。我必须创建一个JSON对象,该对象有两个子对象。一个孩子有一些孩子。如果我的问题令人困惑,请参考下图。它类似于嵌套列表。
例:
所属类别
<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。 注意:一个类别包含多个产品详细信息。这意味着对于特定的CATEGORY_ID,PRODUCT_ID,PRODUCT_NAME和UNIT_COST将重复多次。
我试过的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"}' +
']' +
'}' +
'}' +
']' +
'}';
答案 0 :(得分: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 object literal syntax的子集,因此您应该直接分配对象。
在你的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"
}
]
}
]
}
答案 1 :(得分: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 },
]
};
答案 2 :(得分: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/对过滤结果很有用