简体   繁体   English

遍历JSON数组后,如何获取URL更新为不同的API ID?

[英]How can I get my URL to update to different API id's once I've looped through a JSON array?

When I'm looping through my JSON file I'm taking out all the Ids with this forEach loop 当我循环遍历JSON文件时,我使用此forEach循环取出所有ID。

var gameId = request("https://api.sportradar.us/ncaamb-t3/games/" + yyyy + "/" + mm + "/" + dd + "/schedule.json?api_key=************", function(error, response, body){
    if(!error && response.statusCode == 200){
        var data = JSON.parse(body);
      data.games.forEach((v) => {
        console.log(v.id);
      });
    }
}); 

It gives me the ID's of the information that I want like this 它给我这样我想要的信息的ID

dcb9a3df-a350-4fa3-ba8d-ddb413a5c5df
c4aa937a-0676-4395-856d-5b7e2216027c
91b8cea3-d41d-4269-aac7-b94e4b06d27c
6043b675-64b7-4575-bc0f-1af1502eb366
56083249-a870-40f7-9d86-f221c588d31c
6f0e4992-d5cc-4e40-ba7f-08cf8f801295
a4ca2161-bbcd-4423-8a51-e5886873e78a
1b9cd7bc-8970-4c62-ae72-c03f93cb2c05
21f9601e-db34-4c30-a568-b40f40627e66
0ccb6184-7abc-4035-ad14-6b2c405ee002
7618e36d-4191-4be1-8cdd-61087df2b07b
793e0d40-4f34-462e-ae0c-fde5c0e0aec3
a7da9ccc-80a1-44f8-b8eb-93369f8e8aae
eb02bcae-83f2-4d00-98be-3add44be7c51
a8fd9234-1b37-4b18-abea-f33afcda0ae0
ad536204-057a-4fe6-8f3d-c151018867af
b4c96280-9799-4566-a36e-9c4ac2fb942b
ed0bdb48-3a3b-47f4-b84c-3cdea0122c9e
0a1276d6-0c5d-4c77-9a3c-de6f04a2f342
b9e451c9-e978-40af-b6cf-bcb21f33aec6
c16b8327-5994-45a7-b380-03e1712e6a0c
6a02a4f5-983a-494b-82a8-bd8155256fd8
c0c3061e-814c-4869-ac66-5bbeb8a59830
9a778f37-1010-4625-b715-a7a0222df673
ddcd9846-c8b7-46d5-87c9-526d0431dc13
c75b0bd5-db16-4f8a-81d1-b8e82e7f2dc9
426f3858-a2b0-462c-a72e-57bc5db32a2f
d2cfb022-064b-4fac-ad99-67666ef25a2d
36ced72a-9d6a-47fe-af0b-613f30ac69c0

But then I would like to insert each ID that is returned into a new URL like this: 但是然后我想将返回的每个ID插入新的URL中,如下所示:

request("https://api.sportradar.us/ncaamb-t3/"+ gameId +"/boxscore.json?api_key=***************", function(error, response, body) {
    if(!error && response.statusCode == 200){
        var data = JSON.parse(body);
        var homeName = (data.home.name);
        var awayName = (data.away.name);
        var homeScore = (data.home.points);
        var awayScore = (data.away.points);
        if(homeScore > awayScore){
          console.log(homeName.toUpperCase() + " WIN!");
        }else {
          console.log(awayName.toUpperCase() + " WIN!");
        }
    }

}); 

I'm using the variable gameId inside of my url like this " https://api.sportradar.us/ncaamb-t3/ "+ gameId +"/boxscore.json?api_key=***************" 我在网址中使用了变量gameId,例如““ https://api.sportradar.us/ncaamb-t3/ ” + gameId +“ / boxscore.json?api_key = ********** *****”

But it's only giving me the id's and not what I'm wanting which is whether or not the team won or not. 但这只是给我身份证,而不是我想要的,这就是车队是否赢了。 Do you guys have any idea what I could be doing wrong? 你们知道我在做什么错吗? I need it to visit two different JSON pages. 我需要它来访问两个不同的JSON页面。

EDIT****** Tried this out but it didn't seem to work, though it also didn't give me any errors. 编辑******对此进行了尝试,但它似乎也没有起作用,尽管它也没有给我任何错误。

request("https://api.sportradar.us/ncaamb-t3/games/" + yyyy + "/" + mm + "/" + dd + "/schedule.json?api_key=j6mu95u99hsaayj5etfuzh6w", function(error, response, body){
    if(!error && response.statusCode == 200){
        var data = JSON.parse(body);
      data.games.forEach((v) => {
        request("https://api.sportradar.us/ncaamb-t3/"+ v.id +"/boxscore.json?api_key=j6mu95u99hsaayj5etfuzh6w", function(error, response, body) {
    if(!error && response.statusCode == 200){
        var data = JSON.parse(body);
        var homeName = (data.home.name);
        var awayName = (data.away.name);
        var homeScore = (data.home.points);
        var awayScore = (data.away.points);
        if(homeScore > awayScore){
          console.log(homeName.toUpperCase() + " WIN!");
        }else {
          console.log(awayName.toUpperCase() + " WIN!");
        }
    }

}); 
      });
    }
});  

When I console.logged it it seems to be working, so I'm not sure why it's not console.logging properly...... 当我进行console.logging时,它似乎可以正常工作,所以我不确定为什么它无法正确进行console.logging ......

792e9eff-cece-49bc-a696-a0632702e96e
https://api.sportradar.us/ncaamb-t3/games/792e9eff-cece-49bc-a696-a0632702e96e/boxscore.json?api_key=*********** function (error, response, body) {
    if(!error && response.statusCode == 200){
        var homeName = (data.home.name);
        var awayName = (data.away.name);
        var homeScore = (data.home.points);
        var awayScore = (data.away.points);
        if(homeScore > awayScore){
          console.log(homeName.toUpperCase() + " WIN!");
        }else {
          console.log(awayName.toUpperCase() + " WIN!");
        }
    }

}

For the code in your edit-- 对于您编辑中的代码-

v.id is the piece of information you want by the looks of it, so in your second API request, you should be concatenating v.id, not gameId. v.id是您所需的信息,因此,在第二个API请求中,应串联v.id,而不是gameId。

  request("https://api.sportradar.us/ncaamb-t3/"+ v.id +"/boxscore.json?api_key=j6mu95u99hsaayj5etfuzh6w", function(error, response, body) {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我想要的URL在API的JSON中,我怎样才能获得API的URL? - How can I get the URL for an API if the URL I want is in the API's JSON? React&Redux:我在后台有 State,但无法在我的 JSX 中渲染循环数组 - React&Redux: I have State in the background, but can't render the looped-through Array in my JSX 如何获取正在循环的数组的行号? - How to get row number of an array that's being looped through? 如何为我的脚本创建一个具有 2 个不同标签的数组,以便我可以识别游戏名称和 url 以将其加载到同一页面中? - How do I make an Array for my script with 2 different labels so I can id the game name and url to load it in the same page? 一旦我使用 webpack 构建并缩小它,我如何向通过浏览器运行的库提供 api url? - How I can provide an api url to my library that is run via the browser once I build and minify it with webpack? 如何通过 ID 获取 json 数组中的数据 - How can I get data in json array by ID 我如何获取我的url请求的Json? - How can i get the Json of my url request? Giphy API-一旦我进行了AJAX调用,就无法弄清楚如何调用数据 - Giphy API - can't figure out how to call the data once I've made the AJAX call 我如何将循环开关放入阵列中? - How would I put my looped switches into a array? 如何在不调用键的情况下从对象数组中获取值(我通过互联网搜索找不到与此完全相同/类似的问题) - How to get a value from an Array of object without calling the key (i've searched through the internet can't found the exact/similar problem to this)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM