I'm trying to compile all of my facebook friends user ids, but the output is difficult for me to work with. How can I simplify? It looks like this currently:
{
u'friends': {
u'paging': {
u'next': u'https://graph.facebook.com/556702392/friends?access_token=-MYACCESSTOKEN-&limit=5000&offset=5000&__after_id=100005821213415'
},
u'data': [
{u'name': u'Ian Fung', u'id': u'419972'},
{u'name': u'Jason Turer', u'id': u'420694'},
...
]
}
...
}
This is what I've been able to come up with:
try:
resp = urllib2.urlopen(url)
contents = resp.read()
except urllib2.HTTPError, error:
contents = error.read()
json_encoded = json.loads(contents)
print json_encoded["data"]["id"]
How can I store just the IDs?
It's not entirely clear what data you want, but this list comprehension extracts id
from friends
data
:
>>> json_data = {u'friends': {u'paging': {u'next': u'https://graph.facebook.com/556702392/friends?access_token=-MYACCESSTOKEN-&limit=5000&offset=5000&__after_id=100005821213415'}, u'data': [{u'name': u'Ian Fung', u'id': u'419972'}, {u'name': u'Jason Turer', u'id': u'420694'}]}}
>>> [x['id'] for x in json_data['friends']['data']]
[u'419972', u'420694']
Hope this helps you(and your given data is clumsy and not clear)
import json
from pprint import pprint
json_data=open('json_data')
decoded_data = json.load(json_data)
pprint(decoded_data)
json_data.close()
you can get the json values like this
decoded_data["friends"]["pagind"]["next"]
decoded_data["data"]["name"]
decoded_data["value"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.