如何组合和显示JSON格式的Javascript对象数组

时间:2017-01-24 18:11:27

标签: javascript jquery json

我是一名初学程序员,试图将两个数组合并,我有两个不同的函数ebay();etsy();,并在表格中显示结果。

1.etsy()从jQuery获取其数组:

var b;
(function($){
    $(document).ready(function(){
        $('#etsy-search').bind('submit', function() {
        //Making API call to Etsy to retrieve product arrays
        //...
        //Making an ajax call 
            $.ajax({
                url: etsyURL,
                dataType: 'jsonp',
                success: function(data) {
                    if (data.ok) {
                    //assigned variable b to the data.results
                    //alert(b) returns: [object Object],[object Object]
                        b = (data.results);     
                    //start next function
                        ebay(b);

                    }}});return false;})});})(jQuery);

2.ebay()从Javascript 检索数组,显示数组

    ebay(b){
    //Making API call to Ebay to retrieve product arrays
    //...
                            //function to retrieve ebay arrays
                            function _cb_findItemsByKeywords(root) {
                                //assigns var items the result array
var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || [];
                              //assigned variable a to the items
                              //alert(a) returns: [object Object][object Object]
                                var a = (items);
                                   //combine two arrays together
                                    var result=a.concat(b);
    //alert(result) returns: [object Object],[object Object],[object Object],[object Object],[object Object]                

                            //get all properties values of a Javascript Object
                            //source stackoverflow: http://stackoverflow.com/questions/7306669/how-to-get-all-properties-values-of-a-javascript-object-without-knowing-the-key
                            var keys = Object.keys(result);

                                //keys.length = 5
                                for (var i = 0; i < keys.length; i++) {
                                    var val = result[keys[i]];

                                    alert(val);
                                   //alert returns: [object Object]

                                   document.write(val[i] + "<br>");
                                   //doesn't return anything

                                   //I want to append the results to table here
                                  }
                            } 
    }

两个javascript对象都有多个不同的属性:
Etsy [{"listing_id":123,"title":"etsy","..."}]
易趣[{"itemId":["123"],"title":["ebay"],..,}]
Full Ebay and Etsy JSON result shown here
问题:如何获取两个数组的属性值,我想获得&#34;标题&#34;来自两者的财产并将它们附加到表格中。

编辑:表格格式,我的最终目标是从两个数组中获取标题,图片和价格。

var trHTML = '';
$.each(keys, function(i,item) {
trHTML += '<tr><td>' + '<a href="'
+ item.url +'" target="_blank" style="color: white"><img src="' 
//unsure of the property names such as 'image'
                                + image + '" border="0"></a></td><td>' 
                                + title + '</td><td>' 
                                + price + '</td></tr>';
})
$('#result-table').append(trHTML);  

I am able to append the array seperately, the table format and names are as shown here.
Etsy和Ebay的属性名称不同 enter image description here

0 个答案:

没有答案