Python 向端点发布JSON格式的数据

Python 向端点发布JSON格式的数据,python,json,Python,Json,我想使用python将以下JSON格式的字符串发布到端点: {"station-id": "FMAT2", "lon": "-97.37055556", "value": "8.70", "lat": "32.80805556", "data-type": "PCIRR", "time": "210811", "date": "170417"} 但当我发送数据时,代码本身会向上述字符串的任何单个部分添加引号,如下所示: "{""station-id"": ""FMAT2"", ""lon"":

我想使用python将以下JSON格式的字符串发布到端点:

{"station-id": "FMAT2", "lon": "-97.37055556", "value": "8.70", "lat": "32.80805556", "data-type": "PCIRR", "time": "210811", "date": "170417"}
但当我发送数据时,代码本身会向上述字符串的任何单个部分添加引号,如下所示:

"{""station-id"": ""FMAT2"", ""lon"": ""-97.37055556"", ""value"": ""8.62"", ""lat"": ""32.80805556"", ""data-type"": ""PCIRR"", ""time"": ""210440"", ""date"": ""170417""}"
你能告诉我为什么会发生这样的事情,我怎样才能避免吗。必须说,我得到了400和429的回应。代码如下:

import os
import requests
from os import listdir
from os.path import isfile, join
from timeit import default_timer
start = default_timer()
files_in_dir = [ f for f in listdir('C:/Users/bxr5813/Desktop/Send data to 
map') if isfile(join('C:/Users/bxr5813/Desktop/Send data to map',f)) ]
matching = [s for s in files_in_dir if "json.csv" in s]
if len(matching)==0:
    print "No new report at this time"
for i in range(0,len(matching)):
    filename=matching[i]
    with open(filename,"r") as f:
            content=f.readlines()
            for j in range(0, len(content)):
                line=content[j]
                url = 'https://fathomless-journey-
39482.herokuapp.com/observations'
                headers = {"Content-Type":"application/json", "Accept": 
"text/plain", "X-Api-Key": 'lmENUdfazMd5STedwFgodgts'}
                r = requests.post(url, data=line, headers=headers)
                print r
                print line
                j+=1
                f.close()
    #os.remove(filename)
duration = (default_timer() - start)
print "Runnung time is "+str(duration)+" Seconds that is "+str(duration/60)+" 
minutes"

您可以将JSON字符串放入.csv文件中,文件名应包含JSON.csv。

在请求中使用
data=JSON.dumps(line)
。在服务器端,使用
incoming=json.loads(line)

来解决isuue,我使用以下代码将“”替换为“”。然后,问题解决了

line = line.replace ('""','"')
line=line.replace('"{','{')
line=line.replace('}"','}')

这里没有足够的信息来理解您所说的内容。您如何读取JSON字符串?您如何发送它?请发布一些可运行的代码来说明您所描述的问题。代码已添加。感谢您为什么不使用字典而不是字符串,更容易阅读、维护和编辑…如果JSON是正确的r在文件中,您可以使用json.loads创建一个字典,请求模块可以很好地使用字典……您应该在这里阅读如何使用json模块