gpt4 book ai didi

node.js - 访问 JSON 返回未定义

转载 作者:太空宇宙 更新时间:2023-11-04 01:41:17 24 4
gpt4 key购买 nike

有人可以指出我做错了什么吗?下面是我从 Amazon Webservices 返回的订单商品的 json,我在 React,Node 中使用以下代码来打印QuantityOrdered,但我得到了未定义

    { 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

最佳答案

确保您的 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>

关于node.js - 访问 JSON 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52804812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com