gpt4 book ai didi

python - 解析来自 COVID 曝光网站的 JSON 请求 - 无法打印所需结果

转载 作者:行者123 更新时间:2023-12-02 16:06:29 25 4
gpt4 key购买 nike

我想创建自己的 map 来显示曝光地点。我正在向官方网站发送一个 json 请求,并收到一个如下所示的对象。

Output

文本输出

{"help": "https://discover.data.vic.gov.au/api/3/action/help_show?name=datastore_search", "success": true, "result": {"include_total": true, "resource_id": "afb52611-6061-4a2b-9110-74c920bede77", "fields": [{"type": "int", "id": "_id"}, {"type": "text", "id": "Suburb"}, {"type": "text", "id": "Site_title"}, {"type": "text", "id": "Site_streetaddress"}, {"type": "text", "id": "Site_state"}, {"type": "text", "id": "Site_postcode"}, {"type": "text", "id": "Exposure_date_dtm"}, {"type": "text", "id": "Exposure_date"}, {"type": "text", "id": "Exposure_time"}, {"type": "text", "id": "Notes"}, {"type": "text", "id": "Added_date_dtm"}, {"type": "text", "id": "Added_date"}, {"type": "text", "id": "Added_time"}, {"type": "text", "id": "Advice_title"}, {"type": "text", "id": "Advice_instruction"}, {"type": "text", "id": "Exposure_time_start_24"}, {"type": "text", "id": "Exposure_time_end_24"}, {"type": "text", "id": "dhid"}], "records_format": "objects", "records": [{"_id":1,"Suburb":"Wodonga","Site_title":"Woolworths - White Box Rise Shopping Centre","Site_streetaddress":"Corner Victoria Cross Parade & Kelliher Avenue","Site_state":"VIC","Site_postcode":"3690","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"11:29am - 11:45am","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"15:08:09","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"11:29:00","Exposure_time_end_24":"11:45:00","dhid":"E8H3"},{"_id":2,"Suburb":"Berwick","Site_title":"Casey Superclinic","Site_streetaddress":"50 Kangan Drive\t","Site_state":"VIC","Site_postcode":"3806","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"11:00am - 11:45am","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"13:11:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"11:00:00","Exposure_time_end_24":"11:45:00","dhid":"C5E3"},{"_id":3,"Suburb":"Corio","Site_title":"Kmart - Corio Village Shopping Centre","Site_streetaddress":"Corner Bacchua Marsh & Purnell Road","Site_state":"VIC","Site_postcode":"3214","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"7:00pm - 7:55pm","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"11:35:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"19:00:00","Exposure_time_end_24":"19:55:00","dhid":"C2B4"},{"_id":4,"Suburb":"Bell Park","Site_title":"The Cheesecake Shop Geelong North","Site_streetaddress":"135 Separation Street","Site_state":"VIC","Site_postcode":"3125","Exposure_date_dtm":"2021-09-18","Exposure_date":"18/09/2021","Exposure_time":"2:15pm - 3:15pm","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"11:35:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"14:15:00","Exposure_time_end_24":"15:15:00","dhid":"C4C8"},{"_id":5,"Suburb":"Corio","Site_title":"Watan Supermarket and Halal Butcher","Site_streetaddress":"83C Purnell Road","Site_state":"VIC","Site_postcode":"3214","Exposure_date_dtm":"2021-09-19","Exposure_date":"19/09/2021","Exposure_time":"2:30pm - 3:30pm","Notes":"Case attended venue","Added_date_dtm":"2021-09-24","Added_date":"24/09/2021","Added_time":"11:35:00","Advice_title":"Tier 2 - Get tested urgently and isolate until you have a negative result","Advice_instruction":"Anyone who has visited this location during these times should urgently get tested, then isolate until confirmation of a negative result. Continue to monitor for symptoms, get tested again if symptoms appear.","Exposure_time_start_24":"14:30:00","Exposure_time_end_24":"15:30:00","dhid":"E9Z7"}], "limit": 5, "_links": {"start": "/api/3/action/datastore_search?limit=5&resource_id=afb52611-6061-4a2b-9110-74c920bede77", "next": "/api/3/action/datastore_search?offset=5&limit=5&resource_id=afb52611-6061-4a2b-9110-74c920bede77"}, "total": 533}}

我想做的是获得一个包含“Site_streetaddress”的列表,但我没有成功。

这是我的代码:

import urllib.request
import json
url = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'
response = urllib.request.urlopen(url).read()
jsonResponse = json.loads(response.decode('utf-8'))
print(jsonResponse)

我试过这里的例子,但我没有得到任何结果。 https://www.w3schools.com/python/python_json.asp

最佳答案

请注意,在顶层只有 3 个键。

import urllib.request
import json
url = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'
response = urllib.request.urlopen(url).read()
jsonResponse = json.loads(response.decode('utf-8'))
print([item.get('Site_streetaddress') for item in jsonResponse['result']['records']])

输出

['Corner Victoria Cross Parade & Kelliher Avenue', '50 Kangan Drive\t', 'Corner Bacchua Marsh & Purnell Road', '135 Separation Street', '83C Purnell Road']

作为旁注,我建议使用requests。安装包后

import requests

url = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'
response = requests.get(url)
data = response.json()
print([item.get('Site_streetaddress') for item in data['result']['records']])

关于python - 解析来自 COVID 曝光网站的 JSON 请求 - 无法打印所需结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69310908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com