这可能是一个愚蠢的问题,但是我对此长期以来一直在摸不着头脑。
我正在尝试使用django中的Facepy / social-auth从facebook GraphAPI请求照片信息。
我的视图有以下代码,但是如何将生成的json转换为python对象?
instance = UserSocialAuth.objects.filter(user=request.user).filter(provider='facebook')
graph = GraphAPI(instance[0].extra_data['access_token'])
p=graph.get('me/photos')
Facepy看起来非常好,但文档很差,有没有更好的python facebook sdk与社交认证很好用?
感谢所有建议。
答案 0 :(得分:2)
Facepy返回本机Python对象,而不是JSON。
response = graph.get('me/photos')
for photo in response['data']:
print photo['source']
答案 1 :(得分:0)
您可以使用simplejson的加载函数
from django.utils import simplejson
simplejson.loads(args)
反序列化
s
(包含JSON的str
或unicode
个实例 文档)到Python对象。If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name must be specified. Encodings that are not ASCII based (such as UCS-2) are not allowed and should be decoded to ``unicode`` first. ``object_hook`` is an optional function that will be called with the result of any object literal decode (a ``dict``). The return value of ``object_hook`` will be used instead of the ``dict``. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting). ``parse_float``, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal). ``parse_int``, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float). ``parse_constant``, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN, null, true, false. This can be used to raise an exception if invalid JSON numbers are encountered. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` kwarg.