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.
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()
There are two ways you can "decode" json with Python, after you've parsed it into dicts and lists with the json library.
First, accessing it by indexes, like this:
url_list = [t['entries'][0]['url'] for t in data['windows'][0]['tabs']]
Or, you can iterate over its tree structure. The example function below isn't general purpose, it just illustrates that you need to consider JSON's three different kinds of "nodes" differently when parsing the tree. A key's "value" might be data, a list of child nodes with no keys, or a dict that's basically a new JSON object. You can't just run through checking every node for its name, data, and children like you would with a regular tree.
def depthFirstSearch(self, jsonobj, target, parentKey=None):
if isinstance(jsonobj, dict):
for key, value in jsonobj.items():
if isinstance(value, (dict, list)):
self.depthFirstSearch(value, target, key)
else: # "data" node
if key == target and parentKey not in self.parentsToExclude:
self.results.append(value)
self.parents[parentKey] += 1
if isinstance(jsonobj, list):
for value in jsonobj:
#lists don't have keys, pass along key from last dict
self.depthFirstSearch(value, target, parentKey)
精彩评论