0

I'm trying to get data from JSON via $.getJSON and assign it to a variable, so later I could iterate through it. My code, as of now, looks like this:

var myJson;

$.getJSON("http://www.json-generator.com/api/json/get/ceQSpTWJTS?indent=2", function(json){
  if (json != null){
    console.log("Load successfull");
  };        
  myJson = json;
});

var main = document.getElementsByClassName("main");
var sec = document.getElementsByClassName("sec");
var entries = myJson.friends;

function myF_1(){
  for(var i = 0; i < items.length; i++) {    
    main[i].innerHTML = entries[i].id;              
  }
};

function myF_2(){
  for(var i = 0; i < items.length; i++) {    
    sec[i].innerHTML = entries[i].name;
  }
};

main.innerHTML = myF_1();
sec.innerHTML = myF_2();

Whereas I'm mostly positive that starting from var main = document.getElementsByClassName("main");, the code is correct (it worked before). The thing is, even the $.getJSON part seems to be okay. Console is returning Load Successfull like it's supposed to and all. Error I'm getting is as follows: Uncaught TypeError: Cannot read property 'friends' of undefined, so it's clearly something with my JSON. And my JSON looks like this:

[
  {
    "_id": "56486b9a3fa6be06a434586a",
    "index": 0,
    "guid": "0ae0d91a-f06e-4657-93a2-b36cdce9539c",
    "isActive": true,
    "balance": "$2,013.92",
    "picture": "http://placehold.it/32x32",
    "age": 35,
    "eyeColor": "blue",
    "name": "Gladys Davis",
    "gender": "female",
    "company": "ZOINAGE",
    "email": "gladysdavis@zoinage.com",
    "phone": "+1 (980) 578-2162",
    "address": "942 Sandford Street, Bannock, Wyoming, 7863",
    "about": "Sit laboris est ut est duis aute occaecat do aliqua do elit in culpa. Elit deserunt anim consequat adipisicing sint. Id sit ullamco exercitation dolore ex fugiat minim Lorem sint voluptate.\r\n",
    "registered": "2014-07-18T01:02:49 -02:00",
    "latitude": -39.674779,
    "longitude": 91.675051,
    "tags": [
    "deserunt",
    "ex",
    "sunt",
    "ipsum",
    "sint",
    "culpa",
    "consectetur"
    ],
"friends": [
  {
    "id": 0,
    "name": "Casandra Walter"
  },
  {
    "id": 1,
    "name": "Wolf Medina"
  },
  {
    "id": 2,
    "name": "Florine Osborne"
  }
],
"greeting": "Hello, Gladys Davis! You have 5 unread messages.",
"favoriteFruit": "strawberry"
},

I don't know what to do from here. Why friends is undefined?

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
roonroon
  • 399
  • 5
  • 18
  • firstly, it would be `myJson[0].friends`, secondly, `$.getJSON` is asynchronous, therefore, `myJson` will be undefined at the point where you are using it – Jaromanda X Nov 15 '15 at 11:46
  • After altering `var entries` to ` var entries = myJson[0].friends` I'm getting `Uncaught TypeError: Cannot read property '0' of undefined` :( – roonroon Nov 15 '15 at 11:49
  • you missed "secondly" – Jaromanda X Nov 15 '15 at 11:50
  • So, I gather then that I should declare the var outside the getJSON? But how? – roonroon Nov 15 '15 at 11:51
  • no - see how this has been closed as duplicate - your answers may lie in the link that reveals all about the mysteries of working with asynchronous code in javascript :p – Jaromanda X Nov 15 '15 at 11:52

0 Answers0