简体   繁体   中英

search json returned from api call

I make a call to a currency rates api to use with money.js to convert rates, This is not the problem. I want to pick a specfic rate from the json data returned..

{
"base": "EUR",
"date": "2016-03-31",
"rates": {
    "AUD": 1.4807,
    "BGN": 1.9558,
    "BRL": 4.1174,
    "CAD": 1.4738,
    "CHF": 1.0931,
    "CNY": 7.3514,
    "CZK": 27.051,
    "DKK": 7.4512,
    "GBP": 0.79155,
    "HKD": 8.8282,
    "HRK": 7.5255,
    "HUF": 314.12,
    "IDR": 15024.84,
    "ILS": 4.295,
    "INR": 75.4298,
    "JPY": 127.9,
    "KRW": 1294.88,
    "MXN": 19.5903,
    "MYR": 4.4078,
    "NOK": 9.4145,
    "NZD": 1.6411,
    "PHP": 52.284,
    "PLN": 4.2576,
    "RON": 4.4718,
    "RUB": 76.3051,
    "SEK": 9.2253,
    "SGD": 1.5304,
    "THB": 40.018,
    "TRY": 3.2118,
    "USD": 1.1385,
    "ZAR": 16.7866
    }
    }

Im just wondering for example if I wanted to pull the ZAR exchange rate from this. How could I do this

var res = {
"base": "EUR",
"date": "2016-03-31",
"rates": {
    "AUD": 1.4807,
    "BGN": 1.9558,
    "BRL": 4.1174,
    "CAD": 1.4738,
    "CHF": 1.0931,
    "CNY": 7.3514,
    "CZK": 27.051,
    "DKK": 7.4512,
    "GBP": 0.79155,
    "HKD": 8.8282,
    "HRK": 7.5255,
    "HUF": 314.12,
    "IDR": 15024.84,
    "ILS": 4.295,
    "INR": 75.4298,
    "JPY": 127.9,
    "KRW": 1294.88,
    "MXN": 19.5903,
    "MYR": 4.4078,
    "NOK": 9.4145,
    "NZD": 1.6411,
    "PHP": 52.284,
    "PLN": 4.2576,
    "RON": 4.4718,
    "RUB": 76.3051,
    "SEK": 9.2253,
    "SGD": 1.5304,
    "THB": 40.018,
    "TRY": 3.2118,
    "USD": 1.1385,
    "ZAR": 16.7866
    }
}

then you could do this:

res.rates.ZAR;
var exchange_rate = {
"base": "EUR",
"date": "2016-03-31",
"rates": {
    "AUD": 1.4807,
    "BGN": 1.9558,
    "BRL": 4.1174,
    "CAD": 1.4738,
    "CHF": 1.0931,
    "CNY": 7.3514,
    "CZK": 27.051,
    "DKK": 7.4512,
    "GBP": 0.79155,
    "HKD": 8.8282,
    "HRK": 7.5255,
    "HUF": 314.12,
    "IDR": 15024.84,
    "ILS": 4.295,
    "INR": 75.4298,
    "JPY": 127.9,
    "KRW": 1294.88,
    "MXN": 19.5903,
    "MYR": 4.4078,
    "NOK": 9.4145,
    "NZD": 1.6411,
    "PHP": 52.284,
    "PLN": 4.2576,
    "RON": 4.4718,
    "RUB": 76.3051,
    "SEK": 9.2253,
    "SGD": 1.5304,
    "THB": 40.018,
    "TRY": 3.2118,
    "USD": 1.1385,
    "ZAR": 16.7866
    }
}

var ratelist = exchange_rate["rates"];
console.log(ratelist["ZAR"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM