I am currently studying JSON and found a helpful guide on w3schools.
Here is the code provided in the guide: https://www.w3schools.com/js/tryit.asp?filename=tryjson_ajax
The guide also includes a sample JSON file: https://www.w3schools.com/js/json_demo.txt
<!DOCTYPE html>
<html>
<body>
<h2>Using XMLHttpRequest to retrieve file content.</h2>
<p>The content is in JSON format, easily convertible to a JavaScript object.</p>
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.name;
}
};
xmlhttp.open("GET", "json_demo.txt", true);
xmlhttp.send();
</script>
<p>Check out <a href="json_demo.txt" target="_blank">json_demo.txt</a></p>
</body>
</html>
I have another JSON file example that I want to use here: https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json
Using the same code but changing
document.getElementById("demo").innerHTML = myObj.name;
to
document.getElementById("demo").innerHTML = myObj;
It doesn't seem to display anything besides [object object], and I'm unsure why. Can someone please assist me with this issue? Thank you.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj;
}
};
xmlhttp.open("GET", "https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json", true);
xmlhttp.send();
</script>
</body>
</html>