首页 > 解决方案 > 如何从 json 响应中获取特定的字段值?

问题描述

如何从 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

提前致谢

标签: pythonpython-3.xlistgoogle-bigquerypython-requests

解决方案


像这样的东西应该工作

resp = response.json()
invoice_url = resp['invoice_url']
merchant_name = resp['merchant_name']
print(invoice_url, marchant_name)

推荐阅读