tags:

views:

347

answers:

3

I'm getting my JSON from reddit.com, essentially something like this. I have done quite a bit of reading, but I don't really understand how I can grab the information I want from this JSON (I want a list of the story links). I understand that I can "decode" the JSON into a dictionary, but do I need to recur throughout the JSON to get what I need?

Thanks in advance.

+8  A: 

If you're using Python 2.6 or later, use the built-in json library. Otherwise, use simplejson which has exactly the same interface.

You can do this adaptively without having to check the Python version yourself, using code such as the following:

try:
    import json
except ImportError:
    import simplejson as json

Then, use json.loads() or whatever as appropriate.

Greg Hewgill
Note that an open url gives a file-like object, so `json.load` is probably the appropriate function.
Mike Graham
A: 

I assume that you would parse the data into a dictionary and then extract the relevant bits from it.

You can parse json in python using appropriate python library

stefanB
+1  A: 
import urllib2
import json

u = urllib2.urlopen('http://www.reddit.com/.json')
print json.load(u)
u.close()
Ignacio Vazquez-Abrams