我正在建立一个网站,允许用户输入地址并返回其当选官员。我正在使用Google的Civic Information API来获取结果。执行代码后,它将返回一个JSON文件,其中包含我需要的所有信息。它似乎有两个数组,一个数组用于“办公室”,另一个数组用于“官员”。我处于无法理解如何将两个数组关联在一起并将办公室分配给一个人的问题上。任何帮助将不胜感激。
<html>
<head>
<script src="https://apis.google.com/js/api.js"></script>
<script>
function loadClient() {
gapi.client.setApiKey("Used my key here");
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/civicinfo/v2/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
function execute() {
var address = street.value+" "+city.value+" "+state.value+" "+zip.value;
return gapi.client.civicinfo.representatives.representativeInfoByAddress({
"address": address
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
//console.log("Response", response.result); this works
// Here is where I assume the logic goes to loop through and assign the information to the "demo "tag
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client");
</script>
</head>
<body onload="loadClient()">
<input name="street" type="text" id="street" placeholder="Street Address">
<input name="city" type="text" id="city" placeholder="City">
<input name="state" type="text" id="state" placeholder="State">
<input name="zip" type="text" id="zip" placeholder="Zip">
<button id="calculate" onclick="execute()"> Find Elected Officials
</button>
<P id-="demo">
</P>
</body>
</html>
答案 0 :(得分:0)
查看api文档:https://developers.google.com/civic-information/docs/v2/representatives#resource。在“办公室”下,您将找到条目
offices[].officialIndices[] list List of indices in the officials array of people who presently hold this office
因此,您可以遍历此数组并根据这些索引获取相应的官员。