Jquery 获取JSON对象的值
我有下面的JSON输出,希望根据输入获得特定值Jquery 获取JSON对象的值,jquery,json,Jquery,Json,我有下面的JSON输出,希望根据输入获得特定值 "quotes": { "USDAUD": 1.278342, "USDEUR": 0.908019, "USDGBP": 0.645558, "USDPLN": 3.731504 } 现在我有美元和英镑的用户输入 如何从“引号”中仅获取特定值 我试过如下方法 from = $("#from option:selected").val(); //this is USD to = $("#to option:selected").
"quotes": {
"USDAUD": 1.278342,
"USDEUR": 0.908019,
"USDGBP": 0.645558,
"USDPLN": 3.731504
}
现在我有美元和英镑的用户输入
如何从“引号”中仅获取特定值
我试过如下方法
from = $("#from option:selected").val(); //this is USD
to = $("#to option:selected").val(); //this is GBP
然后我做了一根绳子
output = to + from;
并试图在控制台中获取价值
console.log(json.quotes); //this gives complete output in console
console.log(json.quotes.output); //but with this i could not get value of USDGBP
如何从JSON对象获取所需的值?在javascript中,对象和数组的用法非常相似 因此,将创建的对象作为数组进行查找:
output = from + to;
if (quotes[output]!==undefined)
{
var yourValue=quotes[output];
}
因为输出是一个字符串,您不能仅使用
json.quotes.output
将其放在方括号中,如:
json.quotes[output]
var json={
“引言”:{
“美元兑澳元”:1.278342,
“美元欧元”:0.908019,
“USDGBP”:0.645558,
“USDPLN”:3.731504
}
};
var from='英镑';
var to=‘美元’;
var输出=至+自;
控制台日志(输出);
log(json.quotes)//这将在控制台中提供完整的输出
log(json.quotes[output])//但有了这个,我无法得到USDGBP的价值,这个问题有点不清楚,可能是重复的