0

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);
}
3
  • 2
    books.length will return undefined, because your JSON is not an array, it's an object Commented May 22, 2017 at 2:53
  • If you are the one creating the server response, consider using an array instead of an object, because it makes little sense for it to be an object as it is. Commented May 22, 2017 at 3:20
  • because 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 Commented May 22, 2017 at 3:22

4 Answers 4

2

Since your JSON is an object (not an array), you can use Object.keys() to get all its keys and then iterate over its keys to get the appropiate values:

var books = JSON.parse(this.responseText);
Object.keys(books).forEach(function (index) {
   var book = books[index];
   var div = document.createElement("div");
   var text = document.createElement("p");
   var image = document.createElement("img");
   image.src = 'books/' + book.folder + '/cover.jpg';
   text.innerHTML = book.title;
   div.appendChild(image);
   div.appendChild(text);
   document.getElementById('allbooks').appendChild(div);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Your json is not an array.. so your .length will be undefined

$.each(books, function(i, n) {
    var book = document.createElement("div");
    var text = document.createElement("p");
    var image = document.createElement("img");
    alert(books[""+i+""].folder)
    image.src = "books/" + n.folder + "/cover.jpg";
    text.innerHTML = n.title;

    book.appendChild(image);
    book.appendChild(text);
    document.getElementById("allbooks").appendChild(book);
});

3 Comments

So what library is that? Where's the tag in the OP?
thats jquery.. you can use that or use normal javascript. Jquery will be easy to loop
jquery looks easier to use. but the html code is provided, and I cannot use any libraries... Thank you for your answer though!
0

A literal object hasn't length property.

var books = {
  "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)"
  }
};

Object.keys(books)
    .map(key => books[key])
    .forEach(book => {
        var div = document.createElement("div");
        var text = document.createElement("p");
        var image = document.createElement("img");
    
        image.src = "books/" + book.folder + "/cover.jpg";
        text.innerHTML = book.title;

        div.appendChild(image);
        div.appendChild(text);
    
        document.getElementById("allbooks").appendChild(div);
});
<div id="allbooks"></div>

Comments

0

You are using JSON Object and not an array. And 'length' is not a property for Array.

So it iterate over this, you can use:

for (var bookKey in books) {
    var book = books[bookKey];
    // And create html content here
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.