如何从ImmutableMultiDict中提取数据

时间:2016-11-04 18:40:31

标签: python flask

我收到的数据是ImmutableMultiDict

ImmutableMultiDict([('slim[]', '{"server":[{"status":"SUCCESS"}],"meta":{},"input":{"name":"sample-img.jpg","type":"image/jpeg","size":41319,"width":400,"height":300},"output":{"width":400,"height":300,"image":"data:image/jpeg;base64,/9j/4AAQ....

我将其转换为字典

data = dict(request.form)

转换为dict后,我最终得到了这种格式的数据。

{
'imageUploaded': 
    ['{     
    "server":[{"status":"SUCCESS"}],
    "meta":{},
    "input":{
                "name":"sample-img.jpg",
                "type":"image/jpeg",
                "size":41319,
                "width":400,
                "height":300
            },
    "output":{"width":400,"height":300,"image":"data:image/jpeg;base64,/9j//WmZyP/2Q=="
        },
    "actions":
            {
                "rotation":0,
                "crop":{"x":0,"y":0,"width":400,"height":300,"type":"manual"},
                "size":
                {
                    "width":640,"height":640
                }
            }
        }']
}

我试过了

data = json.dumps(dict(request.form))
a = json.loads(data)
print(a['imageUploaded']['output']['image'])

但是我收到了这个错误

  

TypeError:list indices必须是整数,而不是str

2 个答案:

答案 0 :(得分:2)

那么这是data? (我认为你很漂亮,因为你不能有多行'string'

{
'imageUploaded': 
    ['{    

然后你需要data['imageUploaded'][0]来获取该数组的第一个元素。

这似乎是一个JSON字符串,因此解析

import json
inner_data = data['imageUploaded'][0]
inner_data = json.loads(inner_data)

然后,你可以inner_data['output']['image']

Sample Run

答案 1 :(得分:0)

实际上,您可以使用for循环来迭代字典值,并获取特定值的多字典键。