Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  3] [ 2]  / answers: 1 / hits: 70403  / 12 Years ago, mon, may 7, 2012, 12:00:00

If I have a JSON file named names.json with:



{employees:[
{firstName:Anna,lastName:Meyers},
{firstName:Betty,lastName:Layers},
{firstName:Carl,lastName:Louis},
]}


How can I use its content in javascript?


More From » jquery

 Answers
105

An example how to do this could be:



<html>
<head>
<script type=text/javascript src=http://code.jquery.com/jquery.min.js></script>
<script type=text/javascript>
$(function(){
$.getJSON('names.json',function(data){
console.log('success');
$.each(data.employees,function(i,emp){
$('ul').append('<li>'+emp.firstName+' '+emp.lastName+'</li>');
});
}).error(function(){
console.log('error');
});
});
</script>
</head>
<body>
<ul></ul>
</body>
</html>

[#85723] Sunday, May 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jacquezf

Total Points: 27
Total Questions: 109
Total Answers: 98

Location: Lesotho
Member since Wed, Jun 2, 2021
3 Years ago
;