[英]How do I turn the following data into a Python dict
我是编程和选择Python作为我的第一语言的新手。 我可以通过API进行“POST”,但是如何将下面的响应转换为Python字典呢?
如果要直接打印响应我只得到:
Response [201]
但是,如果我这样做:
for i in response:
print i
我明白了:
{"id":"9e1ebc5d","side":"buy","item":"dinosaur","type":"limit","amount":"1.0000","displayAmount":"1.0000","price":"100","createdTime":"2014-12-24T16:01:15.3404000Z","status":"submitted","metadata":{}}
然而,除非我能弄清楚如何将其转换为Python字典,否则这对我来说仍然是无用的。
使用json
模块中的loads
函数:
import json
# let x be a string contain the JSON encoded data
x = '{"id":"9e1ebc5d", ...}'
# convert to Python dictionary
p = json.loads(x)
# p is now a Python dictionary
print type(p) # prints <type 'dict'>
print p['id'] # prints 9e1ebc5d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.