为一个输出解析 JSON 多维数据

Posted

技术标签:

【中文标题】为一个输出解析 JSON 多维数据【英文标题】:Parse JSON multi dimensional data for one output 【发布时间】:2018-02-21 08:50:22 【问题描述】:

我正在尝试通过 JQuery 解析这些数据:

http://www.mycurrency.net/service/rates

我想通过它进行搜索,找到一个匹配的国家/地区代码,并将其存储在一个名为“代码”的变量中,然后返回该国家/地区的汇率。

对不起,如果这是重复的,我已经查看了答案,它们要么太复杂,要么太简单。

【问题讨论】:

【参考方案1】:

var json = '["currency_code":"AED","rate":3.6727,"code":"AE","name":"United Arab Emirates","currency_code":"AFN","rate":68.51,"code":"AF","name":"Afghanistan","currency_code":"ALL","rate":111.55,"code":"AL","name":"Albania","currency_code":"AMD","rate":477.85,"code":"AM","name":"Armenia","currency_code":"ANG","rate":1.7775,"code":"AW","name":"Aruba","currency_code":"AOA","rate":165.096,"code":"AO","name":"Angola"]'
var Code="AW";
$.each(jQuery.parseJSON(json),function(key,value)
  if(value.code==Code)
   $("body").append("<span> "+value.currency_code+" |</span><span> "+value.rate+" |</span><span> "+value.code+" |</span><span> "+value.name+" </span><hr>");
);
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

【参考方案2】:
Var Code ;  
var Result = new FromData();
 $.each(yourjsonfile, function (key, value) 
        if (youjsonfile.Code = Code)
Result.append(key, value);
    );

我没有太多经验,也无法发表评论,但是您尝试过吗? 这将遍历您的数据比较代码并将其附加到您的 formdata var 然后您可以通过 ajax 发布它

【讨论】:

【参考方案3】:

请参考以下代码。希望有帮助。您可以将数据作为对象数组获取,然后应用 map 和 reduce 以找到所需的键。我刚刚从 URL 中复制了示例数据并准备了这个小 sn-p。

data = [
  "currency_code": "AED",
  "rate": 3.6727,
  "code": "AE",
  "name": "United Arab Emirates"
, 
  "currency_code": "AFN",
  "rate": 68.4958,
  "code": "AF",
  "name": "Afghanistan"
, 
  "currency_code": "ALL",
  "rate": 111.5,
  "code": "AL",
  "name": "Albania"
, 
  "currency_code": "AMD",
  "rate": 477.85,
  "code": "AM",
  "name": "Armenia"
, 
  "currency_code": "ANG",
  "rate": 1.7775,
  "code": "AW",
  "name": "Aruba"
, 
  "currency_code": "AOA",
  "rate": 165.096,
  "code": "AO",
  "name": "Angola"
];


code = 'AE';
arr = [];

arr = data.map(function(e) 
  if (e.code == code)
    return e.rate;
).reduce(function(accumulator, value) 

  if (value == undefined)
    return accumulator;
  else
    return value;
);


console.log(arr);
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

【参考方案4】:

试试这个小提琴https://jsfiddle.net/sq8vfx0x/1/

<p class="code"></div>
<p class="rate"></div>

var o = [
"currency_code":"AED","rate":3.6727,"code":"AE","name":"United Arab Emirates",
"currency_code":"AFN","rate":68.5003,"code":"AF","name":"Afghanistan",
"currency_code":"ALL","rate":111.479,"code":"AL","name":"Albania",
"currency_code":"AMD","rate":477.85,"code":"AM","name":"Armenia"
];

var searchCode = 'AF';
var code;
for(var x = 0; x < o.length; x++)
    if(o[x].code == searchCode)
        code = o[x].code;
        $('.rate').append('<p>' + o[x].rate + '</p>');
    

$('.code').append('<p>' + code + '</p>');

【讨论】:

以上是关于为一个输出解析 JSON 多维数据的主要内容,如果未能解决你的问题,请参考以下文章

如何让golang 把变量解析为json,并输出为文件。

Postgres 9.3 JSON 输出多维对象

Scrapy 解析 JSON 输出

如何轻松地将多维 JSON 解析为 html?

如何将多维数组转换为json对象[关闭]

KettleKettle入门解析