我想解析以下JSON文件,但是我不知道如何解析另一个对象包含的对象。这是我的代码:
var myRequest = new Request('test.json');
fetch(myRequest)
.then(function(response) { return response.json(); })
.then(function(data) {
console.log(data. ??????????);
});
JSON文件:
{
"products": {
"29033669": {
"anzahl": "11x",
"img": "https://static.openfoodfacts.org/images/products/29033669/front_de.3.400.jpg",
"name": "Zitronenteegetränk"
},
"4001686386613": {
"anzahl": "1x",
"img": "https://static.openfoodfacts.org/images/products/400/168/638/6613/front_de.17.400.jpg",
"name": "Haribo Saft Goldbären"
},
"4008400207322": {
"anzahl": "5x",
"img": "https://static.openfoodfacts.org/images/products/400/840/020/7322/front_de.6.400.jpg",
"name": "Kinder Schokolade"
},
"40518152": {
"anzahl": "4x",
"img": "https://static.openfoodfacts.org/images/products/40518152/front_en.3.400.jpg",
"name": "Karamalz classic"
},
"676478232": {
"anzahl": "1x",
"img": "https://cdn4.iconfinder.com/data/icons/aami-web-internet/64/aami18-38-512.png",
"name": "Bananen"
},
"7613035499768": {
"anzahl": "4x",
"img": "https://static.openfoodfacts.org/images/products/761/303/549/9768/front_de.6.400.jpg",
"name": "Choclait Chips Classic"
}
}
}
非常感谢您的帮助! 列维
答案 0 :(得分:0)
首先,您必须了解使用的数据类型console.log(typeof(data))
如果它是字符串,则使用const objData=JSON.parse(data)
将此字符串解析为对象,现在您可以使用data.products
来访问产品。
如果它已经是直接对象类型,则可以使用data.products
访问。