I would like to grab a data from JSON array, which looks like below, and so this.responceText returns this data. And I try to use the data from my Javascript code, but it is not working, and also there is no error message. Where is wrong in my code? Thanks.
{"0":{"folder":"callofthewild","title":"Call of the Wild"},"1":{"folder":"2001spaceodyssey","title":"2001: A Space Odyssey "},"2":{"folder":"hobbit","title":"The Hobbit"},"4":{"folder":"pokemon","title":"I Choose You! (Pokemon Chapter Books)"},"5":{"folder":"alannathefirstadventure","title":"Alanna: The First Adventure (Song of the Lioness #1)"}}
part of my javascript;
var books = JSON.parse(this.responseText);
for (var i = 0; i < books.length; i++) {
var book = document.createElement("div");
var text = document.createElement("p");
var image = document.createElement("img");
image.src = "books/" + books.i.folder + "/cover.jpg";
text.innerHTML = books.i.title;
book.appendChild(image);
book.appendChild(text);
document.getElementById("allbooks").appendChild(book);
}
books.lengthwill returnundefined, because your JSON is not an array, it's an objectbecause your JSON is not an array, it's an object- actually, JSON is a string, what the OP is dealing with is a plain ol' javascript object :p