简体   繁体   English

json连接到request.post python

[英]json concatenate for request.post python

I'm measuring temperature and humidity from a sensor. 我正在从传感器测量温度和湿度。 I want then to post the measures to a server as json strings. 然后,我想将这些措施作为json字符串发布到服务器。 I'm using a python script. 我正在使用python脚本。

For optimisation, I want to group the json strings and post them. 为了优化,我想将json字符串分组并发布。

My code is like this: 我的代码是这样的:

current_time = time.localtime()[0:6]
date = dateformat % current_time[0:6]
temp = str(round(temp, 1))
hum = str(round(hum, 1))

lastest_measure = {'UID': uuid, 'date': date, 'temperature': temp, 'humidity': hum}
r = requests.post(url, data=json.dumps(lastest_measure), headers=headers)
print r

>>> response 200: Good !

But I can't concatenate the lastest_measure to a grouped_measure . 但是我不能将lastest_measure连接到grouped_measure

I tried to concatenate with 我试图与

grouped_measure = dict(group_measure.items() + lastest_measure.items())

but it gives me an error: 但这给了我一个错误:

"AttributeError: 'dict' object has no attribute 'item'" “ AttributeError:'dict'对象没有属性'item'”

I tried to create a string with all the measure (in the correct format, ie: [{...},{...}, ...] and then use the json.dumps 我试图创建一个带有所有小节的字符串(采用正确的格式,即: [{...},{...}, ...] ,然后使用json.dumps

r = requests.post(url, data=json.dumps(grouped_measure), headers=headers)

But I get an error 500 (because the format doesn't inclue the '' between all the values) 但出现500错误(因为格式不包含所有值之间的”)

So my question is how can I concatenate 2 dictionaries in the right format for the resquest.post method ? 所以我的问题是如何为resquest.post方法以正确的格式连接2个字典? (is it a method ???) (这是一种方法吗?)

Thanks in advance ! 提前致谢 ! Cheers, Mat 欢呼声,垫子

The problem is, the POST request is expecting a JSON format, which is essentially like a Python dictionary, only it accepts only specific types and requires the keys to be strings. 问题是,POST请求期望使用JSON格式,该格式本质上类似于Python字典,只是它仅接受特定类型并且要求键为字符串。

This means, if you are doing a time series, and have the same keys in a dictionary from many dictionaries, like a Python dictionary, it will override the given values. 这意味着,如果您正在执行时间序列,并且在许多字典中的字典中具有相同的键(例如Python字典),它将覆盖给定的值。 In short, there is no way to solve the problem using a single POST request for a website that accepts JSON data. 简而言之,对于接收JSON数据的网站,使用单个POST请求就无法解决问题。

To solve this, you can use iteration. 要解决此问题,可以使用迭代。

Here's an arbitrary example, but works for your example: 这是一个任意示例,但适用于您的示例:

Step 1: Get the dictionaries (which you already have) and create a Python list from them. 步骤1:获取字典(您已经拥有)并从中创建一个Python列表。

# arbitrary case
dicts = []
for num in range(20):
    mydict = {i: i**num for i in range(50)}
    dicts.append(mydict)

Now that I have my dictionaries, I can then iterate over them to post the request. 现在我有了字典,然后可以遍历它们以发布请求。

import json
# iterate
for mydict in dicts:
    json_string = json.dumps(mydict)
    r = requests.post(url, data=json_string, headers=headers)

Now, if you can define the dictionaries and post the requests at the same time, this code will be much more efficient. 现在,如果您可以定义字典并同时发布请求,那么此代码将更加高效。

I got this solution: 我得到了这个解决方案:

data = [ ]
def add_DataSet(field1, field2, field3, field4):
     global data
     data.append({"field1":var1, "filed2":var2, "field3":var3, "field4":var4})

then 然后

add_DataSet(field1, field2, field3, field4)

It works perfect with the 它与

r = requests.post(url, data=json.dumps(data), headers=headers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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