我对这个 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"}' +
']' +
'}' +
'}' +
']' +
'}';