python - 在 Python 中通过 POST 发送 JSON 数组
问题描述
我在 python 上有点菜鸟(刚刚学了一些代码看 Youtube 视频)
我正在尝试做的事情:
- 我有一个名为 deposit.json 的文件,其中包含多个 JSON,如下所示
[ {
"locator":"TRANSACAO448",
"storeCode":"loja01",
"deviceCode":"teste01",
"eventDate":"2020-04-01 09:30:53",
"memberIdentification":"04153883506",
"employeeIdentification":"",
"offerCode":"4",
"points":10,
"purchaseValue":1,
"additionalInformation":[
{
"key":"CODMOVPON",
"value":"1"
}
]
},
{
"locator":"TRANSACAO448",
"storeCode":"loja01",
"deviceCode":"teste01",
"eventDate":"2020-04-01 09:30:53",
"memberIdentification":"04153883506",
"employeeIdentification":"",
"offerCode":"4",
"points":10,
"purchaseValue":1,
"additionalInformation":[
{
"key":"CODMOVPON",
"value":"1"
}
]
}]
- 让下面的脚本将帖子发送到实习生 API
with open('deposits.json', 'r') as infile:
# Variable for building our JSON block
json_block = []
for line in infile:
# Add the line to our JSON block
json_block.append(line)
# Check whether we closed our JSON block
if line.startswith('{'):
# Do something with the JSON dictionary
json_dict = json.loads(''.join(json_block))
a = json_dict
# Start a new block
#Request com os parametros para envio, como tipo e url de destino
r = requests.post(url, data=json.dumps(a) ,headers=headers,timeout=60)
print "------------------------------------------------------------------------------------"
print "Request:"
print json.dumps(a)
print "Data_envio:"+data_envio_completa
print (r)
print (r.text)
print "------------------------------------------------------------------------------------"
#salva o resultado em um txt com a data de envio no nome
with open (data_envio+'_log_transacoes.txt','wb') as l:
l.write(json.dumps(a))
l.write('Status: '+str(r.status_code))
l.write(r.text.encode('utf-8'))
json_block = []
问题是: 我需要像 LOP 一样每次发送一个请求,因为这个 API 不接受 [{json},{json}] 仅 {json} 并且像现在这样的代码只发送 1 个请求,不知道有多少JSON文件中有行。
有人可以帮助我吗?
解决方案
您可以使用json
python 中的包来简化代码
import json
with open('./deposits.json.json') as f:
json_file = json.load(f)
print(json_file)
那会让你
[{'locator': 'TRANSACAO448', 'storeCode': 'loja01', 'deviceCode': 'teste01', 'eventDate': '2020-04-01 09:30:53', 'memberIdentification': '04153883506', 'employeeIdentification': '', 'offerCode': '4', 'points': 10, 'purchaseValue': 1, 'additionalInformation': [{'key': 'CODMOVPON', 'value': '1'}]}, {'locator': 'TRANSACAO448', 'storeCode': 'loja01', 'deviceCode': 'teste01', 'eventDate': '2020-04-01 09:30:53', 'memberIdentification': '04153883506', 'employeeIdentification': '', 'offerCode': '4', 'points': 10, 'purchaseValue': 1, 'additionalInformation': [{'key': 'CODMOVPON', 'value': '1'}]}]
加载到您的变量中,该变量是一个 json 数组,然后您可以循环遍历它以{}
单独发送每一行
for entry in json_file:
r = requests.post(url, data=json.dumps(entry) ,headers=headers,timeout=60)
推荐阅读
- python - 如何结束周长计算循环
- android - 如何在 Flutter 的 WebView 中使用 post 方法
- android - 通过我的 Flutter 应用与第三方应用交互
- blazor - 托管的 Blazor wasm 会在它们是处理服务器端页面的控制器操作时不断加载 Blazor 应用程序
- python - 更改 tensorflow 保存模型的 dtype 策略
- python - 如果其他语言尝试解码它们,将多个嵌套的 python 字典/列表转换为 json 会导致问题吗?
- jpa - 本机 sql 与加入自定义选择重整为 Java 标准 API 和 JPA 规范
- pandas - 如何在 PMID 列中使用 TF-IDF
- assembly - 如何决定哪个 x86-64 (SSE) 指令更有效?
- mongodb - MongoDB文本搜索的分数与找到的token的顺序无关