简体   繁体   English

从 jQuery 或 JS 中的 json 对象中提取数据

[英]Extracting data from json object in jQuery or JS

I want to use the currency data provided by https://raw.github.com/currencybot/open-exchange-rates/master/latest.json我想使用https://raw.github.com/currencybot/open-exchange-rates/master/latest.json提供的货币数据

As an initial test, I've created a cut-down version of this as an inline object:作为初始测试,我创建了一个缩减版本作为内联对象:

var obj = [
{
    "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability, or fitness for any purpose; use at your own risk. Other than that, have fun! More info: http://openexchangerates.org/terms/",
    "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Full license info: http://openexchangerates.org/license/",
    "timestamp": 1339036116,
    "base": "USD",
    "rates": {
        "EUR": 0.795767,
        "GBP": 0.645895,
        "JPY": 79.324997,
        "USD": 1
    }
}];

All I want to be able to do is somehow query/grep/filter the json object with, for example, "EUR" as the criteria and have it return a variable called 'rate' with a value of '0.795767' as a result.我想要做的就是以某种方式查询/grep/过滤json对象,例如,以“EUR”为标准,并让它返回一个名为“rate”的变量,其值为“0.795767”。

I've looked at the JQuery grep and filter functions but I can't figure out how to isolate just the 'rates' section of the object and then to get the rate I want.我已经查看了 JQuery grep 和过滤器函数,但我无法弄清楚如何仅隔离对象的“速率”部分,然后获得我想要的速率。

var obj = [
{
    "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability, or fitness for any purpose; use at your own risk. Other than that, have fun! More info: http://openexchangerates.org/terms/",
    "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Full license info: http://openexchangerates.org/license/",
    "timestamp": 1339036116,
    "base": "USD",
    "rates": {
        "EUR": 0.795767,
        "GBP": 0.645895,
        "JPY": 79.324997,
        "USD": 1
    }
}];

obj[0].rates.EUR; // output: 0.795767

or或者

obj[0].rates['EUR']; output: //0.795767

DEMO演示

If you want to isolate rates in another variable and use that variable then try like following:如果您想隔离另一个变量中的速率并使用该变量,请尝试如下操作:

var rates = obj[0].rates;

Now,现在,

rates.EUR;
rates.GBP;

and so on.等等。

You can also use JSON.parse() function of Javascript to convert JSON String into Javascript JSON object.您还可以使用 Javascript 的JSON.parse()函数将 JSON 字符串转换为 Javascript JSON 对象。

var JSONObject = JSON.parse("{'value1' : 1, 'value2' : 2}");
console.log(JSONObject.value1);  // Prints '1'..

data contains FRELD6 try to :数据包含 FRELD6 尝试:

var reldte;
var sdata = JSON.parse(data);
 $(sdata).each(function () {
        reldte = RefineDate(this.FRELD6.toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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