您好,这些代码确实向Steam Web API请求了
const request = require("request");
request({
url: "https://api.steampowered.com/IEconService/GetTradeOffers/v1/?key=MYAPIKEY&get_sent_offers=1&active_only=1&format=json",
json: true
}, (err, responser, body) => {
console.log(body.response['trade_offers_sent']);
});
这就是返回的内容:
{
trade_offers_sent: [
{
tradeofferid: '3974708687',
accountid_other: 82613664,
message: 'test message',
expiration_time: 1587017229,
trade_offer_state: 9,
items_to_give: [Array],
is_our_offer: true,
time_created: 1585807629,
time_updated: 1585807629,
from_real_time_trade: false,
escrow_end_date: 0,
confirmation_method: 2
}
]
}
但是当我试图通过这种方式获取accountid_other的值时:
console.log(body.response['trade_offers_sent'].accountid_other);
返回未定义
这是因为您必须首先将此json作为对象。可能这段代码可以帮助您。
let jsonData = JSON.parse(response.body);
let obj= new Object(jsonData);
obj.trade_offers_sent.forEach(itemChild => {
let accountid_other=itemChild.accountid_other;
console.log(accountid_other);
})