如何从 json 响应中获取特定的字段值?
Posted
技术标签:
【中文标题】如何从 json 响应中获取特定的字段值?【英文标题】:How to fetch specific field value from the json response? 【发布时间】:2020-08-16 08:23:36 【问题描述】:如何从 json 响应中获取并打印发票 url 和商家名称?在 Python 中
这是我的有效载荷
import requests
import csv
from google.colab import files
url = "https://api.vdit.co/v2/invoices"
payload = "\n\t\"external_id\": \"invoice-1597479693\",\n\t\"amount\": 3800000,\n\t\"payer_email\": \"customer@domain.com\",\n\t\"description\": \"Invoice Demo #1234\"\n"
headers =
'Content-Type': 'application/json',
'Authorization': 'Basic sfdfsdfsdfdsfds==',
'Cookie': 'visid_incap_2182539=9txMzsXTSfuzBolYeHI2Ui6hLl4AAAAAQUIPAAAAAABantpi5VKWQTh30yiNhG8y; nlbi_2182539=H15dNsiPqheQ/JGAjjCKbQAAAAAuILnoaVKZa2DtKg9G+o7E; incap_ses_1130_2182539=pdnQReH84gIVq2yWKJGuDz2aN18AAAAABIuVf37dWMch3rIN+1PZJg=='
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text)
回应
"id":"5f38a9e14b73f4203a87465b","external_id":"invoice-1597479693","user_id":"5da7afcf5bf71e7c19d0a722","status":"PENDING","merchant_name":"PT Evi Asia Tenggara","merchant_profile_picture_url":"https://du8nwjtfkinx.cloudfront.net/vdit.png","amount":3800000,"payer_email":"customer@domain.com","description":"Invoice Demo #1234","expiry_date":"2020-08-17T03:37:05.746Z","invoice_url":"https://invoice-staging.vdit.co/web/invoices/5f38a9e14b73f4203a87465b","available_banks":["bank_code":"MANDIRI","collection_type":"POOL","bank_account_number":"8860848092927","transfer_amount":3800000,"bank_branch":"Virtual Account","account_holder_name":"PT ABC ASIA TENGGARA","identity_amount":0,"bank_code":"BRI","collection_type":"POOL","bank_account_number":"2621556085816","transfer_amount":3800000,"bank_branch":"Virtual Account","account_holder_name":"PT ABV ASIA TENGGARA","identity_amount":0,"bank_code":"BNI","collection_type":"POOL","bank_account_number":"880855854767","transfer_amount":3800000,"bank_branch":"Virtual Account","account_holder_name":"PT ABC ASIA TENGGARA","identity_amount":0,"bank_code":"PERMATA","collection_type":"POOL","bank_account_number":"821457925493","transfer_amount":3800000,"bank_branch":"Virtual Account","account_holder_name":"PT ASIA TENGGARA","identity_amount":0,"bank_code":"BCA","collection_type":"POOL","bank_account_number":"1076620926923","transfer_amount":3800000,"bank_branch":"Virtual Account","account_holder_name":"PT ABC ASIA TENGGARA","identity_amount":0],"available_ewallets":[],"should_exclude_credit_card":false,"should_send_email":false,"created":"2020-08-16T03:37:05.804Z","updated":"2020-08-16T03:37:05.804Z","currency":"IDR"
JSON received:
如何从 json 响应中获取并打印发票 url 和商家名称?在 Python 中
我希望结果是这样的,请指导我
发票网址: “https://invoice-staging.vdit.co/web/invoices/5f38a9e14b73f4203a87465b” 商家名称:PT ABC ASIA TENGGARA
提前致谢
【问题讨论】:
response.json()
将返回 dict 然后按键访问。
【参考方案1】:
这样的东西应该可以工作
resp = response.json()
invoice_url = resp['invoice_url']
merchant_name = resp['merchant_name']
print(invoice_url, marchant_name)
【讨论】:
以上是关于如何从 json 响应中获取特定的字段值?的主要内容,如果未能解决你的问题,请参考以下文章
使用 unmarshal 从响应中获取特定的 JSON 字段
Vue.js - 如何通过 axios 响应数据从对象中获取特定字段
如何从 webview 中的 json responseString 获取特定方法