如何将下面对象中的“18.52005000”的“汇率”值分配给JavaScript变量?
{
"Realtime Currency Exchange Rate": {
"1. From_Currency_Code": "USD",
"2. From_Currency_Name": "United States Dollar",
"3. To_Currency Code": "MXN",
"4. To_Currency_Name": "Mexican Peso",
"5. Exchange Rate": "18.52005000",
"6. Last Refreshed": "2018-02-17 12:53:11",
"7. Time Zone": "UTC"
}
}
我知道一点JavaScript - 一个脚本,我使用API进行货币转换率的接口,我只需要费率部分来完成其余的计算。
我正在做这样的事情来创建对象:
var http_response = httpGet(query_url);
var http_response_json = JSON.parse(http_response);
谢谢!
答案 0 :(得分:0)
如果您的Json结构位于变量" test
",则执行test["Realtime Currency Exchange Rate"]["5. Exchange Rate"]
以访问其值"18.52005000"
答案 1 :(得分:0)
像这样,我的好先生:
var rate = http_response_json['Realtime Currency Exchange Rate']['5. Exchange Rate']
答案 2 :(得分:0)
您可以通过两种方式访问对象属性:
object.prop
object['prop']
如果存在有问题或可变的属性名称,则需要选择2。
var data = {
"Realtime Currency Exchange Rate": {
"1. From_Currency_Code": "USD",
"2. From_Currency_Name": "United States Dollar",
"3. To_Currency Code": "MXN",
"4. To_Currency_Name": "Mexican Peso",
"5. Exchange Rate": "18.52005000",
"6. Last Refreshed": "2018-02-17 12:53:11",
"7. Time Zone": "UTC"
}
}
var rate = data["Realtime Currency Exchange Rate"]["5. Exchange Rate"]
console.log(rate)

答案 3 :(得分:0)
在http_response_json
中获得JSON对象之后,您可以执行类似这样的操作来获取Exchange Rate
var http_response_json = {
"Realtime Currency Exchange Rate": {
"1. From_Currency_Code": "USD",
"2. From_Currency_Name": "United States Dollar",
"3. To_Currency Code": "MXN",
"4. To_Currency_Name": "Mexican Peso",
"5. Exchange Rate": "18.52005000",
"6. Last Refreshed": "2018-02-17 12:53:11",
"7. Time Zone": "UTC"
}
};
var value = http_response_json["Realtime Currency Exchange Rate"]["5. Exchange Rate"];
console.log(value);

答案 4 :(得分:0)
var exchangeRate = http_response_json["Realtime Currency Exchange Rate"]["5. Exchange Rate"];