0

这可能是一个愚蠢的问题,但我已经为这个问题挠头太久了。

我正在尝试在 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 与 social-auth 配合得很好?

感谢所有建议。

4

2 回答 2

2

Facepy 返回本机 Python 对象,而不是 JSON。

response = graph.get('me/photos')

for photo in response['data']:
    print photo['source']
于 2012-08-28T08:08:57.950 回答
0

您可以使用 simplejson 的加载功能

from django.utils import simplejson
simplejson.loads(args)

将(包含 JSON 文档的s一个str或实例)反序列化为Python 对象。unicode

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.
于 2012-07-14T08:13:15.407 回答