第一次尝试从JSON文件中检索数据,并且诚实地不知道如何获取我想要获取的对象。正确调用JSON数据的ajax
有人可以告诉我如何获取值如Family Name
或familyAttributes
?我只是不知道如何处理数据。
当前AJAX调用
$.ajax({
type: 'GET',
url: 'https://somesite.com/EloMS.json',
data: { category : 'Touchmonitors' },
dataType: 'json',
success: function(data) {
$.each(data, function(index, element) {
console.log(element);
});
}
});
JSON语法:
{
"products": [
{
"Family Name": "3201L",
"Type": "IDS",
"Size (inches)": 32,
"Aspect Ratio": "16:9",
"Part Number": "E415988",
"Product Description": "ET3201L-8UWA-0-MT-GY-G",
"Marketing Description": "3201L 32-inch wide LCD Monitor, VGA, HDMI & DisplayPort video interface, 01 series enhanced AV, IntelliTouch Plus USB touch controller interface, Worldwide-version, Clear, Gray ",
"Advance Unit Replacement": "",
"Elo Elite": "",
"Package Quantity": 1,
"Minimum Order Quantity": 1,
"List Price": 1800
},
{ ... }
]
"families": [
{
"category": "Touchmonitors",
"types": [
"Desktop",
"Display",
"Open Frame"
],
"image": "",
"familyAttributes": [
{
"type": "Display",
"image": "",
"families": [
{
"familyName": "0700L",
"image": ""
}
]
},
{ ... }
}
]
}
我试过的element.category
几个变化,但一直没能得到比全之外的任何数据对象。
您是否发现删除密钥中的空格有什么好处?另外 - 我不确定什么'index'和'element'在'$ .each()'调用中,只是遵循我找到的一个例子。 – Adjit 2015-03-18 21:08:55
删除空格将允许在代码中使用更简洁的语法。外部'data'对象上的'.each'在这里用处不大,因为您知道这两个对象被称为'products'和'families'。实际上,当传递一个Object时,两个参数实际上应该是'key'和'value' - 'index'和'element'是您在jQuery集合中调用'$ .each'时得到的。 – Alnitak 2015-03-18 21:11:31
有没有指向'data:{category:“Touchmonitors”}它似乎没有做任何事情......我想尝试做的只是获取JSON文件的某些部分,具体取决于我何时需要它们 – Adjit 2015-03-18 21:14:38