0

I'm having trouble with looping Key\Value in JSON jQuery, $.each function
Initially I've got JSON like this:

json = [
        [
         "Exodus Gods and Kings 2014",
         "720p",
         "986MB",
         "6.1",
         false,
         "Christian Bale, Joel Edgerton, John Turturro, Aaron Paul",
         "3 nominations.",
         "UK, USA, Spain",
         "Ridley Scott",
         "2014",
         "http:\/\/4upld.com\/3Azrc",
         "150 \u062f\u0642\u06cc\u0642\u0647",
         "Adam Cooper, Bill Collage, Jeffrey Caine, Steven Zaillian",
         "English"
        ],
        [
         "American Sniper 2014 720p",
         "720p",
         "900 MB",
         "7.4",
         false,
         "Bradley Cooper, Kyle Gallner, Cole Konis, Ben Reed",
         "Won 1 Oscar. Another 8 wins & 30 nominations.",
         "USA",
         "Clint Eastwood",
         "2014",
         "http:\/\/4upld.com\/2eB70",
         "132 \u062f\u0642\u06cc\u0642\u0647",
         "Jason Hall, Chris Kyle (book), Scott McEwen (book), James Defelice (book)",
         "English"
        ]
       ]

And I would like to loop through all the key/value elements inside the JSON (aaa and bbb) and the retrieve the inner JSON arrays for looping again, so I tried:

$.getJSON( "json", function( data ) {
      $.each(data, function(e,p) {
        $(".title").append('<a>'+p[0]+'-'+p[1]+'</a><br>');
    });
  });

How can I execute the loop each time and put the data into the HTML element? So that every time HTML elements are reconstructed.

HTML :

<nav class="nav-bar"></nav>  
<div class="container">
  <div class="movie">
    <div class="cover">
      <img src="././cover.jpg">
    </div>
      <div class="title">
        <a></a>
      </div>        
  </div>
</div>

Demo : Fiddle

Oldskool
  • 34,211
  • 7
  • 53
  • 66
Mohammad Goldast
  • 367
  • 2
  • 4
  • 14
  • Is this task similar: http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure ? – Beri Jun 23 '15 at 12:46
  • 1
    [Works for me](http://jsfiddle.net/arknj9as/), either your data isn't as shown or something is wrong with code you haven't shown. It's unclear what end result you're looking for. – T.J. Crowder Jun 23 '15 at 12:47
  • Given that your data is an array of arrays, what key/value elements are you referring to? Also note that your current code works fine: http://jsfiddle.net/275duk5g/. – Rory McCrossan Jun 23 '15 at 12:47
  • 1
    What is your exact problem? Does the Ajax call fail? Does the parser fail? Do you call the code before the .title element is ready? – epascarello Jun 23 '15 at 12:51

0 Answers0