Last updated 5 years ago
Was this helpful?
JSON是非常常用的数据交换格式,在很多程序脚本执行结果也输出为JSON格式。JSON格式的字符串特别适合转换成Python的字典:
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
如果字符串不是JSON格式,但是也有规则的分割符。则可以使用Python内置的做转换:
>>> import ast >>> ast.literal_eval("{'muffin' : 'lolz', 'foo' : 'kitty'}") {'muffin': 'lolz', 'foo': 'kitty'}