正在解析来自 COVID 曝光网站的 JSON 请求 - 无法打印所需结果
Parsing JSON request from COVID exposure website - Unable to print desired result
我想创建自己的地图来显示曝光地点。我正在向官方网站发送 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_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']])
您需要如下遍历 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'])
上面的答案很好。当从 json
中提取数据时,有时您需要 'peel' 它的层。
我的过程是:
> jsonResponse.keys()
dict_keys(['help', 'success', 'result'])
> jsonResponse['result'].keys()
dict_keys(['include_total', 'resource_id', 'fields', 'records_format', 'records', 'limit', '_links', 'total'])
# and so on ... Finally, I suggest
for i in jsonResponse['result']['records']:
print(i["Site_streetaddress"])
等等。我建议:
参见 https://www.kite.com/python/answers/how-to-extract-a-value-from-json-in-python
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'])
我想创建自己的地图来显示曝光地点。我正在向官方网站发送 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_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']])
您需要如下遍历 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'])
上面的答案很好。当从 json
中提取数据时,有时您需要 'peel' 它的层。
我的过程是:
> jsonResponse.keys()
dict_keys(['help', 'success', 'result'])
> jsonResponse['result'].keys()
dict_keys(['include_total', 'resource_id', 'fields', 'records_format', 'records', 'limit', '_links', 'total'])
# and so on ... Finally, I suggest
for i in jsonResponse['result']['records']:
print(i["Site_streetaddress"])
等等。我建议: 参见 https://www.kite.com/python/answers/how-to-extract-a-value-from-json-in-python
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'])