如何在JSON点命名中传递变量?

时间:2019-11-07 22:18:35

标签: javascript json api postman

我正在使用cryptocompare API在 POSTMAN 中将货币转换为EUR,我有包含128个国家/地区货币的json数据。我想将任何货币转换为 EUR 并在用户以fsyms(来自符号)输入货币时获得PRICE,它可以将其转换为EUR。当我阅读用户可以输入的fsyms时,我将此值存储在一个名为userinput的变量中,问题是当我在 jsonData.DISPLAY.userinput.EUR.PRICE 之间传递此变量时,它无法工作并且在邮递员控制台中显示错误 TypeError:无法读取未定义的属性“ EUR”

Error

POSTMAN TEST

API

https://min-api.cryptocompare.com/data/pricemultifull?fsyms=JPY&tsyms=EUR

邮递员测试

var jsonData = JSON.parse(responseBody);
const paramsString = request.url.split('?')[1];
const eachParamArray = paramsString.split('&');
let params = {};
eachParamArray.forEach((param) => {
    const key = param.split('=')[0];
    const value = param.split('=')[1];
    Object.assign(params, {[key]: value});
});
var userinput = (params.fsyms);// this is object with request params as key value pairs
var data = jsonData.DISPLAY.userinput.EUR.PRICE;
console.log(userinput);

身体

{
    "RAW": {
        "JPY": {
            "EUR": {
                "TYPE": "5",
                "MARKET": "CCCAGG",
                "FROMSYMBOL": "JPY",
                "TOSYMBOL": "EUR",
                "FLAGS": "1026",
                "PRICE": 0.008286298820795936,
                "LASTUPDATE": 1573165368,
                "MEDIAN": 0.008295432268182709,
                "LASTVOLUME": 204.04,
                "LASTVOLUMETO": 1.690736411395203,
                "LASTTRADEID": "1392678441",
                "VOLUMEDAY": 204327208.38121647,
                "VOLUMEDAYTO": 1693116.3058658,
                "VOLUME24HOUR": 220262096.6747532,
                "VOLUME24HOURTO": 1825157.551942048,
                "OPENDAY": 0.00813131941538667,
                "HIGHDAY": 0.008388929943067683,
                "LOWDAY": 0.008098208117154651,
                "OPEN24HOUR": 0.00815626332693239,
                "HIGH24HOUR": 0.008398161990353476,
                "LOW24HOUR": 0.008101851851851851,
                "LASTMARKET": "bitFlyer",
                "VOLUMEHOUR": 1385871.6724710271,
                "VOLUMEHOURTO": 11483.74680537116,
                "OPENHOUR": 0.008300374904680056,
                "HIGHHOUR": 0.00830320074996652,
                "LOWHOUR": 0.008280400605840016,
                "TOPTIERVOLUME24HOUR": 220262096.6747532,
                "TOPTIERVOLUME24HOURTO": 1825157.551942048,
                "CHANGE24HOUR": 0.0001300354938635461,
                "CHANGEPCT24HOUR": 1.5943023005910366,
                "CHANGEDAY": 0.00015497940540926645,
                "CHANGEPCTDAY": 1.9059564320644353,
                "CHANGEHOUR": -0.000014076083884120155,
                "CHANGEPCTHOUR": -0.1695837121306839,
                "SUPPLY": 0,
                "MKTCAP": 0,
                "TOTALVOLUME24H": null,
                "TOTALVOLUME24HTO": null,
                "TOTALTOPTIERVOLUME24H": 0,
                "TOTALTOPTIERVOLUME24HTO": 2.3283064365386963e-10,
                "IMAGEURL": "/media/35309345/no-image.png",
                "CONVERSIONTYPE": "invert_divide",
                "CONVERSIONSYMBOL": "ETH"
            }
        }
    },
    "DISPLAY": {
        "JPY": {
            "EUR": {
                "FROMSYMBOL": "¥",
                "TOSYMBOL": "€",
                "MARKET": "CryptoCompare Index",
                "PRICE": "€ 0.008286",
                "LASTUPDATE": "Just now",
                "MEDIAN": "€ 0.008295",
                "LASTVOLUME": "¥ 204.04",
                "LASTVOLUMETO": "€ 1.69",
                "LASTTRADEID": "1392678441",
                "VOLUMEDAY": "¥ 204,327,208.4",
                "VOLUMEDAYTO": "€ 1,693,116.3",
                "VOLUME24HOUR": "¥ 220,262,096.7",
                "VOLUME24HOURTO": "€ 1,825,157.6",
                "OPENDAY": "€ 0.008131",
                "HIGHDAY": "€ 0.008389",
                "LOWDAY": "€ 0.008098",
                "OPEN24HOUR": "€ 0.008156",
                "HIGH24HOUR": "€ 0.008398",
                "LOW24HOUR": "€ 0.008102",
                "LASTMARKET": "bitFlyer",
                "VOLUMEHOUR": "¥ 1,385,871.7",
                "VOLUMEHOURTO": "€ 11,483.7",
                "OPENHOUR": "€ 0.008300",
                "HIGHHOUR": "€ 0.008303",
                "LOWHOUR": "€ 0.008280",
                "TOPTIERVOLUME24HOUR": "¥ 220,262,096.7",
                "TOPTIERVOLUME24HOURTO": "€ 1,825,157.6",
                "CHANGE24HOUR": "€ 0.00013",
                "CHANGEPCT24HOUR": "1.59",
                "CHANGEDAY": "€ 0.00015",
                "CHANGEPCTDAY": "1.91",
                "CHANGEHOUR": "€ -0.000014",
                "CHANGEPCTHOUR": "-0.17",
                "SUPPLY": "¥ 0",
                "MKTCAP": "€ 0",
                "TOTALVOLUME24H": "¥ NaN",
                "TOTALVOLUME24HTO": "€ NaN",
                "TOTALTOPTIERVOLUME24H": "¥ 0",
                "TOTALTOPTIERVOLUME24HTO": "€ 2.328e-10",
                "IMAGEURL": "/media/35309345/no-image.png",
                "CONVERSIONTYPE": "invert_divide",
                "CONVERSIONSYMBOL": "ETH"
            }
        }
    }
}

0 个答案:

没有答案