Express with API - 无法访问内部 JSON 对象

问题描述 投票:0回答:1

这是来自 Riot Developer API 的代码。

我想在控制台中记录 body 对象的 id。

app.get('/hallo', function(req, res) {
var data = {};
var summoner = "dyrus";
var apikey = "xxxxxxxx";
var URL = "https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by- 
name/Teemo?api_key=" + apikey;
var URL2 = "https://euw1.api.riotgames.com/lol/summoner/v3/summoners/by- 
name/" + summoner +"?api_key=" + apikey;

request(URL, function (err, data) {
  console.log(data.body);
 })
})

写作:

console.log(data.body) 

还给我:

"{"id":34080052,"accountId":37712662,"name":"Teemo","profileIconId":0,"revisionDate":1495767212000,"summonerLevel":6}" 

如何访问对象主体中的

id
字段?

express request riot-games-api
1个回答
0
投票

您可以通过以下方式访问它:

JSON.parse(data.body).id

© www.soinside.com 2019 - 2024. All rights reserved.