如何将以下数据转换为Python dict

时间:2014-12-24 15:59:18

标签: python json

我非常擅长编程并选择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词典,否则这对我来说仍然毫无用处。

1 个答案:

答案 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