简体   繁体   English

使用Jquery或Javascript解析嵌套的Yodlee JSON feed

[英]Parsing nested Yodlee JSON feed using Jquery or Javascript

I am trying to retrieve transaction details from the below Yodlee JSON resultset. 我正在尝试从下面的Yodlee JSON结果集中检索交易详细信息。 I can parse "itemDisplayName" without a problem but having trouble with the jquery code when trying to parse anything nested for example: itemData -> accounts -> cardTransactions -> plainTextDescription 我可以解析“ itemDisplayName”没有问题,但是在尝试解析任何嵌套的内容时遇到了jquery代码的麻烦,例如:itemData-> accounts-> cardTransactions-> plainTextDescription

One dude recommended using Javascript For loops instead of jQuery but not sure... 一个家伙推荐使用Javascript For循环而不是jQuery,但不确定...

var orgs = $.parseJSON(data);    

$(orgs.Body).each(function(i,el) {

            var new_orgs = '<div>' + el.itemDisplayName + '</div>';

            $('#response-getItemSummaries').append(new_orgs);

             });

///RAW YODLEE JSON RESULTSET /// RAW YODLEE JSON结果集

http://pastebin.com/6498mZJf http://pastebin.com/6498mZJf

Thanks in advance! 提前致谢!

instead of .each() you should use $.each() : 而不是.each()您应该使用$.each()

$.each(orgs.Body, function(i,el) {
   var new_orgs = '<div>' + el.itemDisplayName + '</div>';
   $('#response-getItemSummaries').append(new_orgs);
});

Description for $.each() : Description for $.each()

A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. 通用迭代器函数,可用于无缝迭代对象和数组。 Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. 具有长度属性的数组和类似数组的对象(例如函数的arguments对象)通过从0到length-1的数字索引进行迭代。 Other objects are iterated via their named properties. 其他对象通过其命名属性进行迭代。

The $.each() function is not the same as $(selector).each() , which is used to iterate, exclusively, over a jQuery object. $ .each()函数与$(selector).each()不同 ,后者仅用于遍历jQuery对象。 The $.each() function can be used to iterate over any collection, whether it is an object or an array. $ .each()函数可用于遍历任何集合,无论它是对象还是数组。 In the case of an array, the callback is passed an array index and a corresponding array value each time. 对于数组,每次回调都会传递一个数组索引和一个对应的数组值。 (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated. (也可以通过this关键字访问该值,但是Javascript始终将this值包装为Object,即使它是简单的字符串或数字值也是如此。)该方法返回其第一个参数,即被迭代的对象。

Fiddle Demo 小提琴演示


what if I wanted to use $.each() to parse nested arrays such as itemData -> accounts -> cardTransactions -> plainTextDescription form the data feed that I provided? 如果我想使用$ .each()解析嵌套数组(例如itemData-> account-> cardTransactions-> plainTextDescription表单),该怎么办?

Oh its a late reply for your comment but this is what you should do with jQuery: 哦,对您的评论的回复很晚,但这是您应该使用jQuery的方法:

$.each(orgs.Body, function (i, el) {
    if (el.itemData.accounts !== undefined) {
        $.each(el.itemData.accounts, function (i, el) {
            if (el.cardTransactions !== undefined) {
                $.each(el.cardTransactions, function (i, el) {
                    if (el.plainTextDescription !== undefined) {
                        $('body').append(el.plainTextDescription + '<br/>');
                    }
                });
            }
        });
    }
});

Get the plainTextDescription this way with jQuery 通过jQuery以这种方式获取plainTextDescription

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

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