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.
python - json. loads allows duplicate keys in a dictionary, overwriting the first value - Stack Overflow.
The JSON standard recommends that a JSON object not have duplicate field names.
There is no "error" if you use more than one key with the same name, but in JSON, the last key with the same name is the one that is going to be used. In your case, the key "name" would be better to contain an array as it's value, instead of having a number of keys "name".
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.
import urllib2
import json
u = urllib2.urlopen('http://www.reddit.com/.json')
print json.load(u)
u.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With