如何使用javascript从json数组中获取json值

时间:2014-11-18 09:50:16

标签: javascript json

我正在使用javascript从json数组获取值。我是json数组的初学者,我无法理解如何做到这一点。我的json数组是这样的:

{
  "disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",
  "license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",
  "timestamp": 1416297649,
  "base": "USD",
  "rates": {
    "AED": 3.673191,
    "AFN": 58.008151,
    "ALL": 112.1323,
    "AMD": 415.972002,
    "ANG": 1.78696,
    "AOA": 100.644875,
    "ARS": 8.508199,
    "AUD": 1.145932,
    "AWG": 1.79,
    "AZN": 0.7843,
    "BAM": 1.567493,
    "BBD": 2,
    "BDT": 77.32636,
    "BGN": 1.567435,
    "BHD": 0.376895,
    "BIF": 1556.029233,
    "BMD": 1,
    "BND": 1.298167,
    "BOB": 6.908841,
    "BRL": 2.607403,
    "BSD": 1,
    "BTC": 0.0025684709,
    "BTN": 61.7715,
    "BWP": 9.257686,
    "BYR": 10814.633333,
    "BZD": 1.997764,
    "CAD": 1.127204,
    "CDF": 925.406333,
    "CHF": 0.962128,
    "CLF": 0.02436,
    "CLP": 595.853895,
    "CNY": 6.12456,
    "COP": 2154.505,
    "CRC": 538.195095,
    "CUP": 1.000738,
    "CVE": 87.892625,
    "CZK": 22.17167,
    "DJF": 178.03412,
    "DKK": 5.961459,
    "DOP": 43.68968,
    "DZD": 84.73913,
    "EEK": 12.5279,
    "EGP": 7.147473,
    "ERN": 15.062575,
    "ETB": 20.09255,
    "EUR": 0.801147,
    "FJD": 1.934408,
    "FKP": 0.638658,
    "GBP": 0.638658,
    "GEL": 1.76438,
    "GGP": 0.638658,
    "GHS": 3.21655,
    "GIP": 0.638658,
    "GMD": 43.08885,
    "GNF": 7084.038333,
    "GTQ": 7.612101,
    "GYD": 206.740124,
    "HKD": 7.754716,
    "HNL": 21.22432,
    "HRK": 6.145546,
    "HTG": 46.35835,
    "HUF": 245.287701,
    "IDR": 12156.366667,
    "ILS": 3.836499,
    "IMP": 0.638658,
    "INR": 61.74318,
    "IQD": 1176.692567,
    "IRR": 26711.333333,
    "ISK": 123.965999,
    "JEP": 0.638658,
    "JMD": 112.8815,
    "JOD": 0.706622,
    "JPY": 116.5573,
    "KES": 90.07741,
    "KGS": 57.6,
    "KHR": 4059.736567,
    "KMF": 394.713449,
    "KPW": 900,
    "KRW": 1098.851659,
    "KWD": 0.291222,
    "KYD": 0.82625,
    "KZT": 180.936999,
    "LAK": 8049.581667,
    "LBP": 1511.646667,
    "LKR": 130.990299,
    "LRD": 91.50915,
    "LSL": 11.11745,
    "LTL": 2.765696,
    "LVL": 0.562739,
    "LYD": 1.206794,
    "MAD": 8.838571,
    "MDL": 15.071,
    "MGA": 2720.776667,
    "MKD": 49.38793,
    "MMK": 1030.461733,
    "MNT": 1874.666667,
    "MOP": 7.986375,
    "MRO": 290.8235,
    "MTL": 0.683738,
    "MUR": 31.6021,
    "MVR": 15.28127,
    "MWK": 457.6505,
    "MXN": 13.57063,
    "MYR": 3.352459,
    "MZN": 31.100225,
    "NAD": 11.11885,
    "NGN": 172.279401,
    "NIO": 26.61524,
    "NOK": 6.766937,
    "NPR": 99.04372,
    "NZD": 1.258938,
    "OMR": 0.38502,
    "PAB": 1,
    "PEN": 2.931746,
    "PGK": 2.560203,
    "PHP": 44.94794,
    "PKR": 101.784399,
    "PLN": 3.380801,
    "PYG": 4666.459993,
    "QAR": 3.640328,
    "RON": 3.551761,
    "RSD": 96.324459,
    "RUB": 47.07185,
    "RWF": 691.6032,
    "SAR": 3.752252,
    "SBD": 7.443016,
    "SCR": 14.17481,
    "SDG": 5.686665,
    "SEK": 7.409777,
    "SGD": 1.297628,
    "SHP": 0.638658,
    "SLL": 4361.666667,
    "SOS": 786.04194,
    "SRD": 3.275,
    "STD": 19621.866667,
    "SVC": 8.750655,
    "SYP": 168.829401,
    "SZL": 11.11241,
    "THB": 32.79493,
    "TJS": 5.080025,
    "TMT": 2.85,
    "TND": 1.833351,
    "TOP": 1.984081,
    "TRY": 2.228282,
    "TTD": 6.34007,
    "TWD": 30.72224,
    "TZS": 1723.825,
    "UAH": 15.49353,
    "UGX": 2731.136667,
    "USD": 1,
    "UYU": 24.17477,
    "UZS": 2405.860013,
    "VEF": 6.30235,
    "VND": 21329.55,
    "VUV": 98.452501,
    "WST": 2.385222,
    "XAF": 526.037765,
    "XAG": 0.06207936,
    "XAU": 0.00083957,
    "XCD": 2.70156,
    "XDR": 0.682345,
    "XOF": 526.128025,
    "XPF": 95.673269,
    "YER": 214.9703,
    "ZAR": 11.09921,
    "ZMK": 5253.075255,
    "ZMW": 6.373054,
    "ZWL": 322.355006
  }
}

我的javascript函数是这样的:

  function GetBalanceQty(val) {
            //in val u get dropdown list selected value
            var id = val;
            alert("value=" + id);


            var xmlhttp = new XMLHttpRequest();
            var url = "https://openexchangerates.org/api/latest.json?app_id=324d066072324a7fba34618f5c5dfd83";

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    myFunction(xmlhttp.responseText);
                }
            }
            xmlhttp.open("GET", url, true);
            xmlhttp.send();

            function myFunction(response) {
                var arr = JSON.parse(response);


 // Logic to display value of currency (contained in val argument) in jason array.


            }

我的javascript函数GetBalanceQty(val)在val参数中收到3个字母的货币名称,现在我想显示其对应的值。

1 个答案:

答案 0 :(得分:1)

function myFunction(response) {
    var currencyData = JSON.parse(response);

    if (!currencyData.rates) {
        // possibly handle error condition with unrecognized JSON response
        alert("currency data not found!");
    }

    // id variable is available from outer function
    if (id in currencyData.rates) {
        alert(currencyData.rates[id]);
    } else {
        alert("unknown currency code!");
    }
}