<h1> Inserting our JSON Data into and object</h1>
<p> First,we are creating an array to store our data.We are then creating an object with couple of objects inside the object "ListofStudents" </p>
<h2>Traversing the ListofStudents.</h2>
<p>We are creating a variable called output inside for statement to store the data temporarily.we are then using the variable to populate the Div "GraduationList".</p>
<h3><b> Output of this code is: </b></h3>
<br></br>
<div id="GraduationList"></div>
JavaScript
//an array is stored with brackets[]
//object in JSON is stored with curly braces {}
//To organize our data we are creating an object called "ListofStudents".This object has couple of other objects inside {}
var data={"ListofStudents":[
{
"firstName":"Robert",
"lastName":"Fuller",
"Graduated": {
"Year":2008,
"Department":"ComputerScience",
"Grade":"B"
}
},
{
"firstName":"Thomas",
"lastName":"Rivard",
"Graduated": {
"Year":2007,
"Department":"Physics",
"Grade":"A"
}
}
]}
document.getElementById("GraduationList").innerHTML=data.ListofStudents[0].firstName + " " + data.ListofStudents[0].lastName+"--"+ data.ListofStudents[0].Graduated.Year +"--" +data.ListofStudents[0].Graduated.Department +"--"+data.ListofStudents[0].Graduated.Grade;
//Traversing the ListofStudents
//I am using a for statement and creating a variable called output to temporarily store the data.We use the output variable to populate the GraduationList Div.
var output="<ul>";
for (var i in data.ListofStudents) {
output+="<li>" + data.ListofStudents[i].firstName + " " + data.ListofStudents[i].lastName + "--" + data.ListofStudents[i].Graduated.Year+"--" +data.ListofStudents[0].Graduated.Department +"--"+data.ListofStudents[0].Graduated.Grade+"</li>";
}
output+="</ul>";
document.getElementById("GraduationList").innerHTML=output;
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.