So im having issues here, when I do a console check I can get my json file to print out its conent into the console using console.log but now im trying to print this out on the page.
I want to print out the first one called information inside the json out to the screen using innerHTML so it basically looks like this:
The second part need to make the one called websites print out as ul.
This is my js so far:
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(xhr.status === 200) {
console.log(xhr.responseText);
var jsonStr = JSON.parse(xhr.responseText);
document.getElementById("info").innerHTML += Student[0].Namn + ", ";
}
};
xhr.open("GET", "student.json", true);
xhr.send(null);
I just cant seem to reach the object I want and print it out.
This is my json code:
{
"Student": [
{
"Information":
{
"Namn" : "Emil",
"Email" : "emilpalm94@gmail.com",
"City" : "Linköping",
"Website" : "http://studenter.miun.se/~empa1600/"
}
},
{
"Websites": [
{
"Sitename" : "komplett",
"SiteURL" : "https://www.komplett.se/",
"Description" : "Bra sida för köp av teknik"
},
{
"Sitename": "Inet",
"SiteURL": "https://www.inet.se/",
"Description": "Konkurerande sida mot Komplett"
},
{
"Sitename": "SF",
"SiteURL": "https://www.sf.se/",
"Description": "När man ska se bio med polaren"
},
{
"Sitename": "Code Academy",
"SiteURL": "https://www.codecademy.com/",
"Description": "Lär dig koda"
},
{
"Sitename": "Miun",
"SiteURL": "https://www.miun.se/",
"Description": "Här lär vi oss allt"
}
]
}
]
}
I want the first section to print out
Anaother pic of my current look of the site: https://gyazo.com/ef4b02c9474f443747df6fcdaf5537b6
I did manage once to print out with innerHTML but it just said undefined which I dont get why.
How can I get the section called Information to print out like the first pic and then the websites to list in a Li element?
Thanks!
Edit: How can I now get the json array websites to print out? and be like the picture? also so they are clickable links?
This is what I tried so far:
function printOut(){
jsonStr.Student[1].Websites.forEach(w => {
document.getElementById("sites").innerHTML += w.Sitename + "<br>";
});
}
printOut();