如何使用 python 将 json 值从 JSON 数组中一个一个地推送到 API
how to push json values to API one by one from JSON array using python
我有一个 .json
文件,其中包含一些 JSON 数据,如下所示,
main.json
[
{
"App_id": "",
"mobile_No": 8****65128,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
},
{
"App_id": "",
"mobile_No": 70007*****8,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
},
{
"App_id": "",
"mobile_No": 84*****399,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
}
]
我的问题是如何将下面的代码解析为 API
而且我还想一个一个解析值
{
"App_id": "",
"mobile_No": 84****0399,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
}
JSON 值到 API 作为 post 方法使用 python。?
并且还需要从 JSON 数组中调用每个值一次如何使用 python 执行此操作,哪个库最适合这个?
问题更新
我想为每次迭代更新我的 main.json
文件,如下所示
main.json
[
{
"App_id": "",
"mobile_No": 8****65128,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
},
"status":"success"
},
{
"App_id": "",
"mobile_No": 70007*****8,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
},
{
"App_id": "",
"mobile_No": 84*****399,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
},
"status":"success"
}
]
如果成功,我们必须在每个字典中保存 status = success 作为参数值,如果在同一个 main.json 文件中失败,则保存失败。
mycode.py
import json
from numpy import equal
import requests
new_path = 'new.json'
with open(new_path, 'r') as f:
data = json.load(f)
for line in data:
r = requests.post('https://apiuat************', json=line)
line['status'] = 'success'
print(r.json())
您可以在 JSON 中使用数组,就像在 python 中正常使用数组一样。这是示例:
import json
with open("main.json") as f:
data = json.load(f)
for item in data:
requests.post(url, json=data)
尝试
import json
import requests
with open("main.json") as f:
data = json.load(f)
for line in data:
r = requests.post('URL',json=line)
print(r.status_code)
我有一个 .json
文件,其中包含一些 JSON 数据,如下所示,
main.json
[
{
"App_id": "",
"mobile_No": 8****65128,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
},
{
"App_id": "",
"mobile_No": 70007*****8,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
},
{
"App_id": "",
"mobile_No": 84*****399,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
}
]
我的问题是如何将下面的代码解析为 API 而且我还想一个一个解析值
{
"App_id": "",
"mobile_No": 84****0399,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
}
JSON 值到 API 作为 post 方法使用 python。? 并且还需要从 JSON 数组中调用每个值一次如何使用 python 执行此操作,哪个库最适合这个?
问题更新
我想为每次迭代更新我的 main.json
文件,如下所示
main.json
[
{
"App_id": "",
"mobile_No": 8****65128,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
},
"status":"success"
},
{
"App_id": "",
"mobile_No": 70007*****8,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
}
},
{
"App_id": "",
"mobile_No": 84*****399,
"loan_type": 33,
"bank_id": 114,
"latest_status": "Rejected",
"bank_appid": "",
"pan_no": "",
"cust_downloadedapp": {
"date": ""
},
"cust_completedapp": {
"date": ""
},
"rejected": {
"date": "",
"rejection_reason": ""
},
"sanctioned": {
"amount": "",
"date": "",
"tenure": "",
"interest_rate": ""
},
"offer_accepted": {
"date": ""
},
"disbursed": {
"amount": "",
"los_id": "",
"date": ""
},
"status":"success"
}
]
如果成功,我们必须在每个字典中保存 status = success 作为参数值,如果在同一个 main.json 文件中失败,则保存失败。
mycode.py
import json
from numpy import equal
import requests
new_path = 'new.json'
with open(new_path, 'r') as f:
data = json.load(f)
for line in data:
r = requests.post('https://apiuat************', json=line)
line['status'] = 'success'
print(r.json())
您可以在 JSON 中使用数组,就像在 python 中正常使用数组一样。这是示例:
import json
with open("main.json") as f:
data = json.load(f)
for item in data:
requests.post(url, json=data)
尝试
import json
import requests
with open("main.json") as f:
data = json.load(f)
for line in data:
r = requests.post('URL',json=line)
print(r.status_code)