简体   繁体   English

将 API 响应(Json 格式)作为图像文件本地保存到系统

[英]Save the API response (Json format ) as image file locally to system

Below is the api response im getting:::下面是 api 响应我得到:::

{"contentType":"image/jpeg","createdTime":"2021-10-10T11:00:47.000Z","fileName":"Passport_Chris J Passport Color - pp.jpg","id":10144,"size":105499,"updatedTime":"2021-10-10T11:00:47.000Z","links":[{"rel":"self","href":"https://dafzprod.custhelp.com/services/rest/connect/v1.4/CompanyRegd.ManagerDetails/43/FileAttachments/10144?download="},{"rel":"canonical","href":"https://dafzprod.custhelp.com/services/rest/connect/v1.4/CompanyRegd.ManagerDetails/43/FileAttachments/10144"},{"rel":"describedby","href":"https://dafzprod.custhelp.com/services/rest/connect/v1.4/metadata-catalog/CompanyRegd.ManagerDetails/FileAttachments","mediaType":"application/schema+json"}]}

i need to save this file as jpg format locally to my system?我需要将此文件作为 jpg 格式本地保存到我的系统吗? could you please provide me a solution through python你能通过 python 给我一个解决方案吗

You might have to decode the JSON-string (if not already done):您可能必须解码 JSON 字符串(如果尚未完成):

import json
json_decoded = json.loads(json_string)

Afterwards you can get the URL to retrieve and the filename from this JSON-structure之后,您可以获得 URL 以从这个 JSON 结构中检索和文件名

url = json_decoded['links'][0]['href']
local_filename = json_decoded['fileName']

Now you can download the file and save it (as seen here How to save an image locally using Python whose URL address I already know? ):现在您可以下载文件并保存它(见此处如何使用 Python 其 URL 地址我已经知道? ):

import urllib.request
urllib.request.urlretrieve(url, local_filename)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM