python - 无法将 JSON 放入 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}
改成
字段 = {“值”:1,“值 2”:2,“值 3”:3}
为此,您需要:
import json
#Your code to create json_body
client.write_points(json.dumps(json_body), database='database')
推荐阅读
- json - CASS_ERROR_SERVER_INVALID_QUERY:无法将 JSON 字符串解码为映射:org.codehaus.jackson.JsonParseException:意外字符('?'
- react-native - 单击 RadioButton React native 后显示 Faltlist
- javascript - 反应中异步函数中的意外保留字“等待”
- arrays - 将数据对象转换为结构数组
- ios - 在拖放元素上滚动 WKWebView
- python - 通过单击下拉菜单中的不同参数来更新表的值。破折号 Python
- sql - SQL如何更新满足要求的每n行
- angular - 如何编辑引导导航栏?
- fastlane - 在您的系统上找不到 fastlane。您是否运行了“sudo gem install fastlane”?
- javascript - Hls 如何始终从现场播放和暂停视频(最后一段)