循环通过Flask request.json

时间:2017-12-25 17:15:42

标签: python json flask

如何使用Post方法获得的对象循环JSON数组?

app.route(API_BASE_URL + 'Sensor/<string:sensor_name>', methods=['POST'])
def add_value(sensor_name):
    data = request.json()['data']
    for obj in data:
        sensor_value = obj['value']
        dtstamp = obj['timestamp']
        user_name = obj['user']

我的帖子请求:

{"data":[{
    "value" : 24,
    "timestamp" : "2017-02-17 22:22:22",
    "user" : "TE"
},
{
    "value" : 26,
    "timestamp" : "2017-02-17 22:22:22",
    "user" : "TE"
}]}

为什么这不起作用?

我没有从IDE收到错误,但是从Postman我得到了这个错误:

TypeError: 'dict' object is not callable

1 个答案:

答案 0 :(得分:1)

request.json不是一个功能。 .json是保存字典的属性,因此您需要将data = request.json()['data']更改为data = request.json['data']