I have the json file result.json looking as follows:
{
"employees":[
{"firstName":"John","lastName":"Doe" },
{"firstName":"Anna","lastName":"Smith" },
{"firstName":"Peter","lastName":"Jones" }]
}
In my html file I'm trying to use jQuery to access one of the objects from the json file, i.e. one of the persons, and display some of the objects data.
<!DOCTYPE html>
<html>
<body>
<head>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<p id="demo"></p>
<script>
var obj;
$.getJSON("http://insyn.local:666/result.json", function(json){
obj = json;
});
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
</script>
</body>
</html>
But this only gives me the error "Uncaught TypeError: Cannot read property 'employees' of undefined". What am I missing here?