我有一些 JSON,如下图所示。在 items 下有一个名为 0
的对象。
我想获取项目内的属性。如何使用 jQuery 或 Javascript 访问它?
这是我目前所拥有的:
jQuery.getJSON('http://localhost/magento/js/primitives/js/SpaceTreeRegion.json',
function(jsonData) {
jQuery.each(jsonData, function(currentRegionNode, jsonData) {
alert(jsonData.items.product_name);
});
});
这段代码报错:jsonData.items.value is undefined
JSON是这样的:
[
{
"category_id": "Clothes",
"Items": [
{
"product_id": 1,
"price": 50,
"product_name": "Shirts"
},
{
"product_id": 2,
"price": 60,
"product_name": "T-Shirt"
}
]
}
]
我想访问对象中每个 items
的 product_id
、price
和 product_name
。
最佳答案
试试这段代码
jQuery.getJSON('your json url', function(yourJsonData) {
jQuery.each(yourJsonData[0].Items, function(key, customData) {
alert(customData.product_name);
});
});
关于javascript - 使用 JavaScript 从 Json 对象访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20898715/