0

Im trying to learn how to get data from json-data but I cannot figure out how to get the 'title' and 'url' when the json looks like this:

{
"about": "about",
"episodes": [
    {
        "id": 123,
        "title": "the title",
        "pod": {
            "podtitle": "podtitle",
            "url": "url"
        }
    },
    {
        "id": 1234,
        "title": "the title2",
        "pod": {
            "podtitle": "podtitle2",
            "url": "url2"
        }

I am using $.getJSON(jsonURL, function(data) { ... but I am just getting the "top" keys.

How do I get the deeper stuff like 'url' and 'podtitle'?

John
  • 15
  • 3
  • Have you tried `data.episodes[0].pod.url`? – Christian Stewart May 03 '13 at 21:53
  • Note that by the time you access the properties in your code from the `data` variable it is no longer JSON - jQuery has already parsed it, and passed the resulting object to your callback. (Just a terminology quibble.) – nnnnnn May 03 '13 at 22:15
  • Thank you Christian, now that I've got it to work your comment is very clear and is what I was looking for. Im only a beginner though and needed some more context to get it working. Sorry for the duplicate. – John May 03 '13 at 22:56

2 Answers2

0
data.episodes[someIndex].pod.podtitle

You may want to iterate over the episodes some way, either with a good ol' for loop or by using $.each for example.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
0

episodes has t he value of an array, so it has numeric indices:

data.episodes[0].title;
data.episodes[0].pod.url;
data.episodes.forEach(function (elem) {
    elem.title;
});

I also created this so you can hover over element values to see how to access them with JS: http://jsfiddle.net/ExplosionPIlls/yHj5X/1/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Thank you Mr Pills! This made me understand how it works, I think :) Appreciate it! – John May 03 '13 at 22:42