-8

I have some JSON below, What I would like to do is iterate through the JSON file and pull out the 'title' element and make a list. Could anybody help?

{ "slides" : {
        "sl_0_0" : {
            "title" : "Slide 0-0",
            "copy" : "Copy for slide 0-0",
            "link" : "sl_0_0.html"
        },
        "sl_1_0" : {
            "title" : "Slide 1-0",
            "copy" : "Copy for slide 1-0",
            "link" : "sl_1_0.html"
        },
        "sl_1_1" : {
            "title" : "Slide 1-1",
            "copy" : "Copy for slide 1-1",
            "link" : "sl_1_1.html"
        },
        "sl_2_0" : {
            "title" : "Slide 2_0",
            "copy" : "Copy for slide 2_0",
            "link" : "sl_2_0.html"
        },
        "sl_2_1" : {
            "title" : "Slide 2_1",
            "copy" : "Copy for slide 2_1",
            "link" : "sl_2_1.html"
        },
        "sl_2_2" : {
            "title" : "Slide 2_2",
            "copy" : "Copy for slide 2_2",
            "link" : "sl_2_2.html"
        }
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • soooooo... you want us to write your code? what have you tried so far? what language are you using?... we don't have a lot to go on here... – Patrice Oct 21 '14 at 13:19
  • Which programming language are you using ? – Ashraf Bashir Oct 21 '14 at 13:27
  • No, I wasn't expecting you to write my code. I haven't used JSON before so was just looking for some leaders: – Lee Marshall Oct 21 '14 at 13:29
  • Here's what I have so far: var info = JSON.parse(request.responseText); var output = ''; for (var i = 0; i <= Object.keys(info.slides).length-1; i++) { // for (key in info.slides[i]) { // if (info.slides[i].hasOwnProperty(key)) { // output += '
  • ' + // '' + key + ''; // '
  • '; // } // }; alert(Object.keys(info.slides)[i].title); // output += '
  • ' + // '' + key + ''; // '
  • '; }; – Lee Marshall Oct 21 '14 at 13:30
  • 3
    Add the code into your question. It's not readable as a comment. – esqew Oct 21 '14 at 14:00
  • And what's the problem with the code you have? – Felix Kling Oct 21 '14 at 14:00
  • Its not returning the desired data. I can loop through 'slides' using Object.keys(info.slides), but this returns a string, returning 'sl_0_0', 'sl_1_1' etc. For each of these I then need to dive down for the title. So info.slides[0].title returns an undefined. – Lee Marshall Oct 21 '14 at 14:21
  • Have a look at http://stackoverflow.com/q/85992/218196 and http://stackoverflow.com/q/11922383/218196. – Felix Kling Oct 21 '14 at 15:00
  • Worked it out, thanks people: for (var i = 0; i <= Object.keys(info.slides).length-1; i++) { var str = Object.keys(info.slides)[i]; output += '
  • ' + '' + info.slides[str].title + '' + '
  • '; }; – Lee Marshall Oct 21 '14 at 15:58
  • for (var i = 0; i <= Object.keys(info.slides).length-1; i++) { var str = Object.keys(info.slides)[i]; output += '
  • ' + '' + info.slides[str].title + '' + '
  • '; }; – Lee Marshall Oct 21 '14 at 16:00