I plan to connect to a url, http://od.moi.gov.tw/api/v1/rest/datastore/A01010000C-000628-023, to get data with JSON format. How to parse JSON string vi Python, and get specific data?
How-to
Here has sample code:
import urllib.request, json from test.json.row import row import os os.environ['http_proxy'] = 'http://proxy.cht.com.tw:8080' with urllib.request.urlopen("http://od.moi.gov.tw/api/v1/rest/datastore/A01010000C-000628-023") as url: # return with dict data type data = json.loads(url.read().decode()) dataList = [] count = 1 # 取得 result / records 下的資料 for value in data['result']['records']: # Skip the first record if(count > 1): no = value['No'] # 編號 address = value['Address'] # 地點位置 deptNm = value['DeptNm'] # 管轄警察局 branchNm = value['BranchNm'] # 分局 dataList.append(row(no, address, deptNm, branchNm)) print('no={}, address={}, deptNm={}, branchNm={}'.format(no, address, deptNm, branchNm)) count += count
Reference
[1] https://data.gov.tw/dataset/6247
No comments:
Post a Comment