working with json files

hello. i have two json files. one has titles inside, the other has paragraphs. i used a for loop within a for loop to get the first title from the first json file, then the first paragraph from the second json file, then the second title, etc. no matter what i do all i'm getting are the titles printed out first. the paragraphs get printed out after the last title or not at all. on and off i've been at this for over a week and it's making me sick.

Tagged:

Answers

  • To help we'd need to see the data structure of each file. Depending on this you should probably be able to extract everything in a single loop, but I can't be sure till I see how it's being stored. A few entries from each file will be enough, or links to their location online.

  • Post your latest attempt to parse the file too. We can then explain why that's not working ;)

  • function setup() {
      loadJSON("lige.json", n);
      // meaningless function name!
      function n(n) {
        for (var i = 0; i < n.lige.length; i++) {
          println(n.lige[i].naziv);
          loadJSON("ponude.json", p);
          // function declaration in a loop is BAD!
          // you create an instance of the function each iteration...
          function p(p) {
            var d = n.lige[0].razrade[0].ponude;
            for (var j = 0; j < d.length; j++) {
              println(p[j].naziv);
              // so now what do you do? \:)
            }
          }
        }
      }
    }
    
    
    
    function draw() {
    
    }
    
  • edited December 2015
    var podaci;
    var naslovi;
    
    function preload() {
      podaci = loadJSON("ponude.json");
      naslovi = loadJSON("lige.json");
    }
    
    function setup() {
      noCanvas();
      nogomet();
    }
    
    function nogomet() {
      createDiv(naslovi.lige[0].naziv);
      createElement('table');
      for (var i = 0; i < podaci.tecajevi.length; i++) {
        createElement('div', podaci[i].tecajevi[i].naziv);
        for (var j = 0; j < 4; j++) {
          createElement('tr', podaci[j].tecajevi[j].naziv);
          createElement('td', podaci[j].broj);
          createElement('td', podaci[j].naziv);
          createElement('td', podaci[j].vrijeme);
          createElement('td', podaci[j].tecajevi);
        }
      }
    } // why am i getting TypeError podaci.tecajevi is undefined?
    
  • This is my initial implementation:

    var ligeData, ponudeData;
    
    function setup() {
      loadJSON("lige.json", function(data) {
        // store data for later use
        ligeData = data;
    
        loadJSON("ponude.json", function(data) {
          // store data for later use
          ponudeData = data;
          allDataLoaded();
        });
      });
    }
    
    
    
    function draw() {
    
    }
    
    
    function allDataLoaded() {
    
      var ponudeLookupTable = {},
          ligeArray = ligeData.lige;
    
      // populate lookup table
      // This will speed up retrieval of ponude items since we'll be able to use a
      // direct ID reference later
      for(var i=0, len= ponudeData.length; i<len; i++) {
        // separated out for clarity
        var ponudeCode = ponudeData[i].id;
        // store ponude.id with its equivalent index in ponudeData
        // We can then later get the matching ponudeData[index] with
        // for example: ponudeLookupTable['8991909']
        ponudeLookupTable[ponudeCode] = i;
      }
    
      for(var i=0, lLen = ligeArray.length; i<lLen; i++) {
        var item = ligeArray[i],
            ponude = item.razrade[0].ponude,
            title = item.naziv;
    
            console.info(i, title);
    
        for(var j=0, pLen = ponude.length; j<pLen; j++) {
          // separated out for clarity
          var ponudeIndex = ponudeLookupTable[ponude[j]],
              thisPonude = ponudeData[ponudeIndex];
              console.info(thisPonude);
        }
    
    
      }
    
      // To figure out the structure of your data you can explore directly via browser console with the following
      // console.log(ligeData);
      // console.log(ponudeData);
    }
    

    I don't have time to explain this in detail right now (already spent longer than I should!). If you have questions let me know...

Sign In or Register to comment.