jQuery .each显示数组数据 - 无法解决它

时间:2014-05-14 01:03:51

标签: javascript jquery arrays json

我试图遍历所有这些数组数据,但是它不知道如何使用.each函数通过jquery显示所有数据?有人可以帮助我吗?

ARRAY:

{
    "ListOrdersResult": {
        "Orders": {
            "Order": [
                {
                    "ShipmentServiceLevelCategory": "Standard",
                    "OrderTotal": {
                        "Amount": "29.00",
                        "CurrencyCode": "GBP"
                    },
                    "ShipServiceLevel": "Std UK Dom",
                    "LatestShipDate": "2013-11-28T23:59:59Z",
                    "MarketplaceId": "A1F83G8C2ARO7P",
                    "SalesChannel": "Amazon.co.uk",
                    "ShippingAddress": {
                        "Phone": "0800 000 0000",
                        "PostalCode": "A11 H11",
                        "Name": "stephanie ross",
                        "CountryCode": "GB",
                        "StateOrRegion": "regiion",
                        "AddressLine2": "cairnbulg",
                        "AddressLine1": "loco 2222 name",
                        "City": "fraserburgh"
                    },
                    "ShippedByAmazonTFM": "false",
                    "OrderType": "StandardOrder",
                    "FulfillmentChannel": "MFN",
                    "BuyerEmail": "c9tkdmn724jpgkd@blahblah.com",
                    "OrderStatus": "Shipped",
                    "BuyerName": "custom A Ross",
                    "LastUpdateDate": "2013-11-27T14:26:53Z",
                    "EarliestShipDate": "2013-11-27T00:00:00Z",
                    "PurchaseDate": "2013-11-26T22:25:39Z",
                    "NumberOfItemsUnshipped": "0",
                    "AmazonOrderId": "205-8108202-4976362",
                    "NumberOfItemsShipped": "1",
                    "PaymentMethod": "Other"
                },
                {
                    "ShipmentServiceLevelCategory": "Standard",
                    "OrderTotal": {
                        "Amount": "29.00",
                        "CurrencyCode": "GBP"
                    },
                    "ShipServiceLevel": "Std UK Dom",
                    "LatestShipDate": "2013-11-28T23:59:59Z",
                    "MarketplaceId": "A1F83G8C2ARO7P",
                    "SalesChannel": "Amazon.co.uk",
                    "ShippingAddress": {
                        "Phone": "0800 000 0000",
                        "PostalCode": "A11 H11",
                        "Name": "stephanie ross",
                        "CountryCode": "GB",
                        "StateOrRegion": "regiion",
                        "AddressLine2": "cairnbulg",
                        "AddressLine1": "loco 2222 name",
                        "City": "fraserburgh"
                    },
                    "ShippedByAmazonTFM": "false",
                    "OrderType": "StandardOrder",
                    "FulfillmentChannel": "MFN",
                    "BuyerEmail": "c9tkdmn724jpgkd@blahblah.com",
                    "OrderStatus": "Shipped",
                    "BuyerName": "custom A Ross",
                    "LastUpdateDate": "2013-11-27T14:26:53Z",
                    "EarliestShipDate": "2013-11-27T00:00:00Z",
                    "PurchaseDate": "2013-11-26T22:25:39Z",
                    "NumberOfItemsUnshipped": "0",
                    "AmazonOrderId": "205-8108202-4976362",
                    "NumberOfItemsShipped": "1",
                    "PaymentMethod": "Other"
                }
            ]
        },
        "CreatedBefore": "2014-05-14T01:12:05Z"
    },
    "ResponseMetadata": {
        "RequestId": "46f5c980-91e6-44d3-bc9d-668976855862"
    },
    "xmlns": "https://mws.amazonservices.com/Orders/2011-01-01"
}

CURRENT JS:

$(document).ready(function(){
  $.get('functions/ListOrders.php', function(xml){ 
    var newOrders = $.xml2json(xml);
    $.each(newOrders.Orders.Order, function(index, value) { 
      console.log(value);
      console.log(value.ShipmentServiceLevelCategory);
    });
    $('body').text(JSON.stringify(newOrders));
  });
});

1 个答案:

答案 0 :(得分:0)

您缺少JSON对象的第一个元素:

更改

 $.each(newOrders.Orders.Order, function(index, value) { 

 $.each(newOrders.ListOrdersResult.Orders.Order, function(index, value) { 

演示: http://jsfiddle.net/9YU3H/