【发布时间】:2017-04-08 11:51:22
【问题描述】:
我在JSONArray 中有一个JSONObject(如下面的 JSON)。现在我必须将productID 值添加为对象的KEY,如Required JSON。我正在尝试通过javascript来做到这一点。但我无法获得所需的输出。
我试图在 Google 上找到任何解决方案。但这行不通。 谁能指导我如何做到这一点或向我展示任何参考资料?
原始 JSON
[
{
"productID": "C05M01P001",
"productName": "Aashirvaad Atta - Whole Wheat (முழு கோதுமை)",
"productUnit": "1 kg",
"productPrice": "Rs.50.00",
"productSellingPrice": "",
"productSID": "",
"productImage": "http://www.thechert.com/image/cache/Product_Images_2016/asvd-attaplain-atta-large-500x500.jpg",
"productDescription": "Ashirvaad Atta - Whole Wheat 1 kg..",
"productCID": "C05",
"productMID": "M01"
},
..... it goes on
]
必需的 JSON
{
"C05M01P001" : {
"productID": "C05M01P001",
"productName": "Aashirvaad Atta - Whole ",
"productUnit": "1 kg",
"productPrice": "Rs.50.00",
"productSellingPrice": "",
"productSID": "",
"productImage": "http://www.thechenart.com/image/cache/Product_Images_2016/asvd-attaplain-atta-large-500x500.jpg",
"productDescription": "Ashirvaad Atta - Whole Wheat 1 kg",
"productCID": "C05",
"productMID": "M01"
},
..... it goes on
}
【问题讨论】:
-
结果不是有效的javascript。
-
“必需的 JSON”无效。这不是 JSON,而是对象数组
-
你
required JSON可以是一个对象,而不是一个数组。 -
您的结果不能是您建议的结果,但可以是对象数组,例如
[ { "C05M01P001: {...}}, {"C05M01P002": {...}} ]或(可能更有意义)单个对象{"C05M01P001: {...}, "C05M01P002": {...}}- 你需要哪个? -
抱歉给您带来了困惑。正如 Err Hunter 所建议的,它是一个 JSON 对象。
标签: javascript arrays json object