出于某种原因,我无法使用price
变量从此JSON中提取:
{
"list": {
"meta": {
"type": "resource-list",
"start": 0,
"count": 1
},
"resources": [{
"resource": {
"classname": "Quote",
"fields": {
"change": "-0.979900",
"chg_percent": "-1.955109",
"day_high": "49.290001",
"day_low": "48.200001",
"issuer_name": "Delta Air Lines, Inc.",
"issuer_name_lang": "Delta Air Lines, Inc.",
"name": "Delta Air Lines, Inc. Common St",
"price": "49.140099",
"symbol": "DAL",
"ts": "1458663972",
"type": "equity",
"utctime": "2016-03-22T16:26:12+0000",
"volume": "7921714",
"year_high": "52.770000",
"year_low": "34.610000"
}
}
}]
}
}
我正在使用:this.list.resources.resource.fields.price
但它无效
答案 0 :(得分:2)
resources
是一个数组,因此您需要通过索引访问它:
this.list.resources[0].resource.fields.price;
这显然假设数组中只有1个条目。如果有多个,你需要循环它们:
for (var i = 0; i < this.list.resources.length; i++) {
var price = this.list.resources[i].resource.fields.price;
// do something with the price here...
}
另请注意,由于此值是一个价格,您可能需要考虑使用toFixed(2)
将其强制为2位小数,但请注意,这会将类型强制转换为字符串,因此请确保执行任何操作事先对它进行计算。