无法将 JSON 放入 InfluxDB

Unable to put JSON into InfluxDB

尝试将数据放入 InfluxDB 时我感到很沮丧。我该怎么做呢?我有这样的数据:

fields = {'value': 1, 'value2': 2, 'value3': 3}

我想给它添加一个日期时间戳,所以我这样设置:

json_body = [
    {
        "timestamp": "2018-10-30",
        "fields": fields
    }
]

那我用

client.write_points(json_body, database='database')

这给了我一个错误 Unable to Parse : missing fields。我也尝试了很多不同的东西,例如:

json_body = {}
json_body["timestamp"] = "2018-10-30"
json_body["fields"] = fields
client.write_points(json_body, database='database') # returns 'str' has no obj attribute 'get'
client.write_points([json_body], database='database') # returns unable to parse : missing fields

有人可以指出我做错了什么以及如何解决吗?

JSON 使用双引号。因此,您需要在字段字典中将单引号更改为双引号。

字段 = {'value': 1, 'value2': 2, 'value3': 3}

改为

字段 = {"value": 1, "value2": 2, "value3": 3}

为此,您需要:

import json

#Your code to create json_body

client.write_points(json.dumps(json_body), database='database')