我有一个从ajax调用返回的JSON对象,似乎无法解析这些值

时间:2018-01-19 00:40:40

标签: javascript jquery json ajax



var data = JSON.parse('{"orders":[{"orderId":654654654,"orderNumber":"5555555","orderKey":"000000000000-0000000000000","orderDate":"2018-01-18T07:17:58.0000000","createDate":"2018-01-18T07:26:29.1600000","modifyDate":"2018-01-18T07:26:30.0100000","paymentDate":"2018-01-18T07:17:59.0000000","shipByDate":null,"orderStatus":"awaiting_shipment","customerId":55555555,"customerUsername":"username","customerEmail":"email@here.com","billTo":{"name":"JOHN DOE","company":null,"street1":null,"street2":null,"street3":null,"city":null,"state":null,"postalCode":null,"country":null,"phone":null,"residential":null,"addressVerified":null},"shipTo":{"name":"JOHN DOE","company":null,"street1":"123 AVE","street2":"LOGISTICS","street3":null,"city":"MIAMI","state":"FL","postalCode":"33166","country":"US","phone":"333-333-3333","residential":false,"addressVerified":"Address validated successfully"},"items":[{"orderItemId":654654654654,"lineItemKey":"654654654654654","sku":"","name":"IBM Series X3850 X5 4x E7-8870 2.4ghz 40-Cores / 128gb / 8x 146gb / M5015","imageUrl":"https://i.ebayimg.com/5654654654F","weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"quantity":1,"unitPrice":1139.99,"taxAmount":0.00,"shippingAmount":null,"warehouseLocation":null,"options":[],"productId":null,"fulfillmentSku":null,"adjustment":false,"upc":null,"createDate":"2018-01-18T07:26:29.16","modifyDate":"2018-01-18T07:26:29.16"}],"orderTotal":1228.28,"amountPaid":1228.28,"taxAmount":0.00,"shippingAmount":88.29,"customerNotes":null,"internalNotes":null,"gift":false,"giftMessage":null,"paymentMethod":"PayPal","requestedShippingService":"FedExHomeDelivery","carrierCode":"fedex","serviceCode":"fedex_home_delivery","packageCode":"package","confirmation":"none","shipDate":null,"holdUntilDate":null,"weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"dimensions":{"units":"inches","length":35.00,"width":23.00,"height":15.00},"insuranceOptions":{"provider":null,"insureShipment":false,"insuredValue":0.0},"internationalOptions":{"contents":null,"customsItems":null,"nonDelivery":null},"advancedOptions":{"warehouseId":65465465,"nonMachinable":false,"saturdayDelivery":false,"containsAlcohol":false,"mergedOrSplit":false,"mergedIds":[],"parentId":null,"storeId":4654654654,"customField1":null,"customField2":null,"customField3":null,"source":null,"billToParty":null,"billToAccount":null,"billToPostalCode":null,"billToCountryCode":null,"billToMyOtherAccount":null},"tagIds":[654654],"userId":null,"externallyFulfilled":false,"externallyFulfilledBy":null,"labelMessages":null}],"total":1,"page":1,"pages":1}');



var orderId = data.orders[0].orderId;

console.log(orderId);




这是对象:

{"orders":[{"orderId":654654654,"orderNumber":"5555555","orderKey":"000000000000-0000000000000","orderDate":"2018-01-18T07:17:58.0000000","createDate":"2018-01-18T07:26:29.1600000","modifyDate":"2018-01-18T07:26:30.0100000","paymentDate":"2018-01-18T07:17:59.0000000","shipByDate":null,"orderStatus":"awaiting_shipment","customerId":55555555,"customerUsername":"username","customerEmail":"email@here.com","billTo":{"name":"JOHN DOE","company":null,"street1":null,"street2":null,"street3":null,"city":null,"state":null,"postalCode":null,"country":null,"phone":null,"residential":null,"addressVerified":null},"shipTo":{"name":"JOHN DOE","company":null,"street1":"123 AVE","street2":"LOGISTICS","street3":null,"city":"MIAMI","state":"FL","postalCode":"33166","country":"US","phone":"333-333-3333","residential":false,"addressVerified":"Address validated successfully"},"items":[{"orderItemId":654654654654,"lineItemKey":"654654654654654","sku":"","name":"IBM Series X3850 X5 4x E7-8870 2.4ghz 40-Cores / 128gb / 8x 146gb / M5015","imageUrl":"https://i.ebayimg.com/5654654654F","weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"quantity":1,"unitPrice":1139.99,"taxAmount":0.00,"shippingAmount":null,"warehouseLocation":null,"options":[],"productId":null,"fulfillmentSku":null,"adjustment":false,"upc":null,"createDate":"2018-01-18T07:26:29.16","modifyDate":"2018-01-18T07:26:29.16"}],"orderTotal":1228.28,"amountPaid":1228.28,"taxAmount":0.00,"shippingAmount":88.29,"customerNotes":null,"internalNotes":null,"gift":false,"giftMessage":null,"paymentMethod":"PayPal","requestedShippingService":"FedExHomeDelivery","carrierCode":"fedex","serviceCode":"fedex_home_delivery","packageCode":"package","confirmation":"none","shipDate":null,"holdUntilDate":null,"weight":{"value":1280.00,"units":"ounces","WeightUnits":1},"dimensions":{"units":"inches","length":35.00,"width":23.00,"height":15.00},"insuranceOptions":{"provider":null,"insureShipment":false,"insuredValue":0.0},"internationalOptions":{"contents":null,"customsItems":null,"nonDelivery":null},"advancedOptions":{"warehouseId":65465465,"nonMachinable":false,"saturdayDelivery":false,"containsAlcohol":false,"mergedOrSplit":false,"mergedIds":[],"parentId":null,"storeId":4654654654,"customField1":null,"customField2":null,"customField3":null,"source":null,"billToParty":null,"billToAccount":null,"billToPostalCode":null,"billToCountryCode":null,"billToMyOtherAccount":null},"tagIds":[654654],"userId":null,"externallyFulfilled":false,"externallyFulfilledBy":null,"labelMessages":null}],"total":1,"page":1,"pages":1}

要在我返回的json对象中获取orderId值,我使用: (data.order)是对象。

data.order[0].orders.orderId 

我尝试的任何东西都没有返回值或未定义。 如果我输出data.order对象,我显然得到整个JSON对象。 我在这里阅读了几个主题并且似乎找到了一个像我正在处理的那样复杂的对象/数组。任何帮助表示赞赏。

我删除了返回数组以使其变暗。 这是我的功能,以防你好奇。 如果您不了解我使用Symfony框架和Twig的奇怪标记,那就更好。

function getOrderInfo( orderId, location ){
    var data = {
        id : orderId
    };
    $.ajax({
            type: "POST",
            url: "{{ path('Dashboard_OrderTracker_orderInfo') }}",
            data: data
        })
        .done(function (data) {
            var jsondata = JSON.parse(data);
            var orderNumber = jsondata.orders[0].orderNumber;
            var name = jsondata.orders[0].shipTo.name;
            var phone = jsondata.orders[0].shipTo.phone;
            var customerEmail = jsondata.orders[0].customerEmail;

            $('#info').html( 
                    "<b>Order number:</b> " + orderNumber + "<br/>" +
                    "<b>Customer Name:</b> " + name + "<br/>" +
                    "<b>Cutomer Phone:</b> " + phone + "<br/>" +
                    "<b>Customer Email:</b> <a href='mailto:" + customerEmail + ">" + customerEmail + "</a><br/>"                       
            );
            $( "#info" ).dialog("option", "position", {
                of: location
            }).dialog("open");
        })
        .fail(function ( xhr.responseText ) {
            alert(xhr);
        });

};

1 个答案:

答案 0 :(得分:0)

假设数据是您的对象变量...访问orderId的正确方法是:

data.orders[0].orderId

我建议美化您的JSON对象,以便您可以更好地看到它。