0

I am trying to extract the values from a nested JSON file that looks like so:

var mymenu = {"menu": [{"page": {"url": "http://foo.bar.com","random stuff": {"junk": "rubbish"}}},{"page": {"feed": "http://foo.bar.com"}},{"menu": [{"submenu": [{"page": {"feed": "http://foo.bar.com"}}]}]}]};

The keys I am trying to extract are the feeds with contain urls. I have tried a for...in loop into the retrieved JSON but I can only get as far as pulling the object that the feed is in resulting in the stringified object as a whole. Is there a way to get just the keys I need from the JSON file?

1 Answers1

0

I just validated your json on JSONLint and it seems to be invalid.

Parse error on line 7:
...                   }            }    
----------------------^
Expecting 'STRING'

Try this json, and remember to assign a name to your variable.

var mymenu = {"menu": [{"page": {"url": "http://foo.bar.com","random stuff": {"junk": "rubbish"}}},{"page": {"feed": "http://foo.bar.com"}},{"menu": [{"submenu": [{"page": {"feed": "http://foo.bar.com"}}]}]}]};

Next iterating through the json isn't too bad, just think of it as a multi dimensional array with key value pairs. For instance, if you're running firefox it's a good idea to get firebug.

for(var i = 0; i < mymenu['menu'].length; i)
{
    console.log(mymenu['menu'][i]);
}
classicjonesynz
  • 4,012
  • 5
  • 38
  • 78