简体   繁体   中英

How do I turn the following data into a Python dict

I'm very new to programming and picking Python as my first language. I am able to do a "POST" via an API but how do I convert the response I get below to a Python dictionary?

If were to print the response directly I only get:

Response [201]

But if I were to do:

for i in response:
    print i

I get:

{"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":{}}

However this is still pretty much useless to me unless I can figure out how convert this into a Python dict.

Use the loads function from the json module:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM