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

文本输出
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* {"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
发布于 2021-09-24 07:17:26
您需要遍历您的jsonResponse,如下所示:
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'))
for data in jsonResponse['result']['records']:
print ('Site_streetaddress: '+data['Site_streetaddress'])发布于 2021-09-24 07:16:13
请注意,在顶层只有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']])发布于 2021-09-24 07:30:05
import requests
URL = 'https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=afb52611-6061-4a2b-9110-74c920bede77&limit=5'
with requests.Session() as session:
r = session.get(URL)
r.raise_for_status()
for _r in r.json()['result']['records']:
print(_r['Site_streetaddress'])https://stackoverflow.com/questions/69310908
复制相似问题