Fetching JSON game data
Here is an example of how you could use the fetch
function to retrieve data from https://6away.org/data.json API and log the enemies:
async function getEnemies() {
const response = await fetch('https://6away.org/adventure.json');
const data = await response.json();
for (const enemy of data.enemies) {
console.log(enemy);
}
}
getEnemies();
This code makes an HTTP GET request to the https://6away.org/adventure.json
API, which returns a JSON object containing data for a role-playing adventure game. The object is then parsed and the enemies
array is logged to the console.
You can also use the .then()
method to handle the response, like this:
fetch('https://6away.org/adventure.json')
.then(response => response.json())
.then(data => {
for (const enemy of data.enemies) {
console.log(enemy);
}
});
Last updated