Get guys,
I've tried looking through a bunch of tutorials but I can't seem to find one that covers this in detail. Maybe somebody could point me int he right direction.
I have a .json file that looks like this:
{
"users": [{
"firstName": "John",
"lastName": "Doe"
}, {
"firstName": "Sabrina",
"lastName": "Doe"
}]
}
I want to run a .each loop and grab all of the users. Here's what I've tried:
$.getJSON("database.json", function(data) {
$.each(data.users, function(key, val) {
$('.dbUL').append('<li>' + key + ' : ' + val + '</li>');
});
});
So it looks like it actually does spit out the array since I get an output of 0 : [object Object] and 1 : [object Object].
My question is, how do I dig into the array and actually spit out the objects that I have stored in my array?
Knowing the properties what you want get (fistName, lastName
): jsBin
$.getJSON("database.json", function(data) {
$.each(data.users, function(idx, val) {
$('.dbUL').append('<li>' + val.firstName + ' : ' + val.lastName + '</li>');
});
});
Not knowing the properties you want to get: jsBin
$.getJSON("database.json", function(data) {
var LIhtml = "";
$.each(data.users, function(idx, obj) {
LIhtml += "<li>";
for(var key in obj){
if(obj.hasOwnProperty(key)){
LIhtml += obj[key]+" ";
}
}
LIhtml += '</li>';
});
$('.dbUL').append(LIhtml);
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다