简体   繁体   中英

Can't get specific element of API response [JS]

I can't seem to get a specific part out of the response below, using the code that is also below. I get an "undefined" in the console log when I try to use response["minecraft.net"] . Can someone explain to me why this isn't working?

Response:

"[{"minecraft.net":"green"},{"session.minecraft.net":"green"},{"account.mojang.com":"green"},{"auth.mojang.com":"green"},{"skins.minecraft.net":"green"},{"authserver.mojang.com":"green"},{"sessionserver.mojang.com":"green"},{"api.mojang.com":"green"},{"textures.minecraft.net":"green"},{"mojang.com":"green"}]" 

Code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mojang Servives Status</title>
</head>

<body>
<script>
var api = new XMLHttpRequest();
api.open("GET", "https://status.mojang.com/check", false);
api.send();
console.log(api);
var response = JSON.parse(api.response)
console.log(response["minecraft.net"]);
</script>
</body>

</html>

The response is wrapped inside of an array. So you'd use response[0]['minecraft.net']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM