Python 中的字符串到字典
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4917006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String to Dictionary in Python
提问by LunaCodeGirl
So I've spent way to much time on this, and it seems to me like it should be a simple fix. I'm trying to use Facebook's Authentication to register users on my site, and I'm trying to do it server side. I've gotten to the point where I get my access token, and when I go to:
所以我花了很多时间在这上面,在我看来这应该是一个简单的修复。我正在尝试使用 Facebook 的身份验证在我的网站上注册用户,并且我正在尝试在服务器端进行。我已经到了获得访问令牌的地步,当我去:
https://graph.facebook.com/me?access_token=MY_ACCESS_TOKEN
https://graph.facebook.com/me?access_token=MY_ACCESS_TOKEN
I get the information I'm looking for as a string that's like this:
我得到我正在寻找的信息作为一个字符串,如下所示:
{"id":"123456789","name":"John Doe","first_name":"John","last_name":"Doe","link":"http:\/\/www.facebook.com\/jdoe","gender":"male","email":"jdoe\u0040gmail.com","timezone":-7,"locale":"en_US","verified":true,"updated_time":"2011-01-12T02:43:35+0000"}
{"id":"123456789","name":"John Doe","first_name":"John","last_name":"Doe","link":"http:\/\/www.facebook.com\/jdoe","gender":"male","email":"jdoe\u0040gmail.com","timezone":-7,"locale":"en_US","verified":true,"updated_time":"2011-01-12T02:43:35+0000"}
It seems like I should just be able to use dict(string)on this but I'm getting this error:
似乎我应该能够使用dict(string)它,但我收到此错误:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
ValueError: dictionary update sequence element #0 has length 1; 2 is required
So I tried using Pickle, but got this error:
所以我尝试使用 Pickle,但得到了这个错误:
KeyError: '{'
KeyError: '{'
I tried using django.serializersto de-serialize it but had similar results. Any thoughts? I feel like the answer has to be simple, and I'm just being stupid. Thanks for any help!
我尝试使用django.serializers反序列化它但有类似的结果。有什么想法吗?我觉得答案必须很简单,而我只是愚蠢。谢谢你的帮助!
采纳答案by Cameron
This data is JSON! You can deserialize it using the built-in jsonmoduleif you're on Python 2.6+, otherwise you can use the excellent third-party simplejsonmodule.
这个数据是JSON!如果你使用的是 Python 2.6+,你可以使用内置json模块反序列化它,否则你可以使用优秀的第三方simplejson模块。
import json # or `import simplejson as json` if on Python < 2.6
json_string = u'{ "id":"123456789", ... }'
obj = json.loads(json_string) # obj now contains a dict of the data
回答by Fred Nurk
Use ast.literal_evalto evaluate Python literals. However, what you have is JSON (note "true" for example), so use a JSON deserializer.
使用ast.literal_eval评估 Python 文字。但是,您拥有的是 JSON(例如注意“true”),因此请使用 JSON 反序列化器。
>>> import json
>>> s = """{"id":"123456789","name":"John Doe","first_name":"John","last_name":"Doe","link":"http:\/\/www.facebook.com\/jdoe","gender":"male","email":"jdoe\u0040gmail.com","timezone":-7,"locale":"en_US","verified":true,"updated_time":"2011-01-12T02:43:35+0000"}"""
>>> json.loads(s)
{u'first_name': u'John', u'last_name': u'Doe', u'verified': True, u'name': u'John Doe', u'locale': u'en_US', u'gender': u'male', u'email': u'[email protected]', u'link': u'http://www.facebook.com/jdoe', u'timezone': -7, u'updated_time': u'2011-01-12T02:43:35+0000', u'id': u'123456789'}

