我非常擅长编程并选择Python作为我的第一语言。我可以做一个" POST"通过API,但我如何将下面的响应转换为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词典,否则这对我来说仍然毫无用处。
答案 0 :(得分:5)
使用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