有人可以指出我在做什么错吗?下面是我从亚马逊网络服务返回的用于订购商品的json,我在react,node中使用以下代码来打印订购数量,但是我变得未定义
{ OrderItems:
{ OrderItem:
{ QuantityOrdered: '1',
Title: 'X',
ShippingTax: [Object],
PromotionDiscount: [Object],
ConditionId: 'New',
IsGift: 'false',
ASIN: 'X',
SellerSKU: 'X',
OrderItemId: 'X',
ShippingDiscountTax: [Object],
ProductInfo: [Object],
GiftWrapTax: [Object],
QuantityShipped: '0',
ShippingPrice: [Object],
GiftWrapPrice: [Object],
ConditionSubtypeId: 'New',
ItemPrice: [Object],
ItemTax: [Object],
ShippingDiscount: [Object],
PromotionDiscountTax: [Object] } },
AmazonOrderId: 'X,
ResponseMetadata: { RequestId: 'X' },
Headers:
{ 'x-mws-quota-max': 'unknown',
'x-mws-quota-remaining': 'unknown',
'x-mws-quota-resetson': 'unknown',
'x-mws-timestamp': 'X',
'content-type': 'text/xml',
'content-charset': 'unknown',
'content-length': '2172',
'content-md5': 'unknown',
date: 'Sun, 14 Oct 2018 17:00:05 GMT' },
StatusCode: 200 }
下面是我的代码
var Obj = response;
console.log(Obj);
if (typeof Obj !== 'undefined') {
var objectValue = Obj;
console.log(objectValue.OrderItem.QuantityOrdered);
}
错误:
console.log(objectValue.OrderItem.QuantityOrdered);
^
TypeError: Cannot read property 'QuantityOrdered' of undefined
答案 0 :(得分:0)
确保您的JSON有效,您提供的响应中存在几个错误。修复后,您将可以像这样访问响应:
(function() {
var __awsJSON = {
OrderItems: {
OrderItem: {
QuantityOrdered: '1',
Title: 'X',
IsGift: 'false',
}
},
AmazonOrderId: 'X',
Headers: {
date: 'Sun, 14 Oct 2018 17:00:05 GMT'
},
StatusCode: 200,
};
document.getElementById('quantity-ordered').textContent = __awsJSON.OrderItems.OrderItem.QuantityOrdered;
document.getElementById('amazon-id').textContent = __awsJSON.AmazonOrderId;
document.getElementById('date').textContent = __awsJSON.Headers.date;
document.getElementById('status-code').textContent = __awsJSON.StatusCode;
})();
<pre>__awsJSON.OrderItems.OrderItem.QuantityOrdered</pre>
<div>Quantity ordered: <span id="quantity-ordered"></span></div>
<pre>__awsJSON.AmazonOrderId</pre>
<div>Amazon ID: <span id="amazon-id"></span></div>
<pre>__awsJSON.Headers.date</pre>
<div>Headers ~ Date: <span id="date"></span></div>
<pre>__awsJSON.StatusCode</pre>
<div>Status Code: <span id="status-code"></span></div>