我做了以下jquery从google civics api获取数据。
var dd;
dataloop = function(dd){
for (i in dd.officials) {
console.log( dd.officials[i].name);
for (j in dd.name[i].address) {
console.log(dd.name[i].address[j]);
}
}
};
dataloop;
var tt = function(){
$(document).ready(function() {
$("#officials").click(function(){
$.getJSON("https://www.googleapis.com/civicinfo/v2/representatives?address=40790+W+Wade+Dr%2C+Maricopa%2C+AZ+85138%2C+USA&key=xxx", function(result){
$.each(result, function(i, field){
$("#lobby").append(
JSON.stringify(field),
window.localStorage.setItem('GovtData', field),
document.getElementById('lobby').innerHTML = field[0],
//document.getElementById('lobby').innerHTML = JSON.stringify(field[3]),
//document.getElementById('lobby').innerHTML = JSON.stringify(field.offices.levels[0]),
dataloop(field),
console.log(globalStringAddress),
dd = field[4],
console.log(dd),
);
});
});
});
});
};
当前,它的响应是将其打印到我的目标div上,这很好。原始数据意味着进步,但我真想以任何对象表示法来访问它。您可以看到我也尝试在顶部进行循环。仍然回来生。
感谢您抽出宝贵的时间对此进行检查。如果您认为回购有助于理解,我可以发送链接。
[object Object][{"name":"Donald J. Trump","address":[{"line1":"The White House","line2":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","zip":"20500"}],"party":"Republican Party","phones":["(202) 456-1111"],"urls":["http://www.whitehouse.gov/"],"photoUrl":"https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/PE%20Color.jpg","channels":[{"type":"GooglePlus","id":"+whitehouse"},{"type":"Facebook","id":"whitehouse"},{"type":"Twitter","id":"potus"},{"type":"YouTube","id":"whitehouse"}]},{"name":"Mike Pence","address":[{"line1":"The White House","line2":"1600 Pennsylvania Avenue NW","city":"Washington","state":"DC","zip":"20500"}],"party":"Republican Party","phones":["(202) 456-1111"],"urls":["http://www.whitehouse.gov/"],"photoUrl":"https://www.whitehouse.gov/sites/whitehouse.gov/files/images/45/VPE%20Color.jpg","channels":[{"type":"GooglePlus","id":"+whitehouse"},{"type":"Facebook","id":"whitehouse"},{"type":"Twitter","id":"VP"}]},{"name":"Martha McSally","address":[{"line1":"B40D Dirksen Senate Office Building","city":"Washington","state":"DC","zip":"20510"}],"party":"Republican Party","phones":["(202) 224-2235"],"urls":["https://www.mcsally.senate.gov/"],"emails":["contact@mcsally.senate.gov"],"channels":[{"type":"Twitter","id":"SenMcSallyAZ"}]},{"name":"Kyrsten Sinema","address":[{"line1":"825B&C Hart Senate Office Building","city":"Washington","state":"DC","zip":"20510"}],"party":"Democratic Party","phones":["(202) 224-4521"],"urls":["https://www.sinema.senate.gov/"],"emails":["contact@sinema.senate.gov"],"channels":[{"type":"Twitter","id":"SenatorSinema"}]},{"name":"Tom O'Halleran","address":[{"line1":"324 Cannon House Office Building","city":"Washington","state":"DC","zip":"20515"}],"party":"Democratic Party","phones":["(202) 225-3361"],"urls":["https://ohalleran.house.gov/"],"photoUrl":"https://ohalleran.house.gov/sites/ohalleran.house.gov/files/styles/medium/public/wysiwyg_uploaded/2016-11-17_FAS_0246_1.jpg?itok=Ro0ep4-x","channels":[{"type":"Facebook","id":"repohalleran"},{"type":"Twitter","id":"RepOHalleran"},{"type":"YouTube","id":"repohalleran"}]},{"name":"Doug Ducey","address":[{"line1":"1700 West Washington Street,","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 542-4331"],"emails":["engage@az.gov"],"channels":[{"type":"Twitter","id":"dougducey"},{"type":"YouTube","id":"UCi_TVbfG4I5SK6GhjYEfyVQ"},{"type":"Facebook","id":"dougducey"}]},{"name":"Venden \"Vince\" Leach","address":[{"line1":"1700 W WASHINGTON ST RM 303","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 926-3106"],"emails":["vleach@azleg.gov"]},{"name":"Bret Roberts","address":[{"line1":"1700 W WASHINGTON ST RM 344","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 926-3158"],"emails":["broberts@azleg.gov"]},{"name":"Mark Finchem","address":[{"line1":"1700 W WASHINGTON ST RM 310","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 926-3122"],"emails":["mfinchem@azleg.gov"]},{"name":"Mark Brnovich","address":[{"line1":"2005 N Central Ave","city":"Phoenix","state":"AZ","zip":"85004"}],"party":"Republican Party","phones":["(602) 542-5025"],"urls":["https://www.azag.gov/"],"emails":["aginfo@azag.gov"],"channels":[{"type":"Twitter","id":"GeneralBrnovich"}]},{"name":"Joe Hart","address":[{"line1":"1700 West Washington","line2":"4th Floor;","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 542-5971"]},{"name":"Kimberly Yee","address":[{"line1":"1700 West Washington Street,","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 542-7800"],"emails":["info@aztreasury.gov"],"channels":[{"type":"Twitter","id":"KimberlyYeeAZ"}]},{"name":"Ann A. Scott Timmer","address":[{"line1":"1501 W. Washington","line2":"Suite 402","city":"Phoenix","state":"AZ","zip":"85007"}],"phones":["(602) 452-3396"],"urls":["https://www.azcourts.gov/meetthejustices/JusticeAnnAScottTimmer.aspx"],"emails":["scclerk@courts.az.gov"],"channels":[{"type":"Facebook","id":"ArizonaSupremeCourt"},{"type":"Twitter","id":"AZCourts"}]},{"name":"John Pelander","address":[{"line1":"1501 W. Washington","line2":"Suite 402","city":"Phoenix","state":"AZ","zip":"85007"}],"phones":["(602) 452-3396"],"urls":["https://www.azcourts.gov/meetthejustices/JusticeJohnPelander.aspx"],"emails":["scclerk@courts.az.gov"],"channels":[{"type":"Facebook","id":"ArizonaSupremeCourt"},{"type":"Twitter","id":"AZCourts"}]},{"name":"Clint Bolick","address":[{"line1":"1501 W. Washington","line2":"Suite 402","city":"Phoenix","state":"AZ","zip":"85007"}],"phones":["(602) 452-3396"],"urls":["https://www.azcourts.gov/meetthejustices/Justice-Clint-Bolick"],"emails":["scclerk@courts.az.gov"],"channels":[{"type":"Facebook","id":"ArizonaSupremeCourt"},{"type":"Twitter","id":"AZCourts"}]},{"name":"Scott Bales","address":[{"line1":"1501 W. Washington","line2":"Suite 402","city":"Phoenix","state":"AZ","zip":"85007"}],"phones":["(602) 452-3396"],"urls":["https://www.azcourts.gov/meetthejustices/ChiefJusticeScottBales.aspx"],"emails":["scclerk@courts.az.gov"],"channels":[{"type":"Facebook","id":"ArizonaSupremeCourt"},{"type":"Twitter","id":"AZCourts"}]},{"name":"Robert M. Brutinet","address":[{"line1":"1501 W. Washington","line2":"Suite 402","city":"Phoenix","state":"AZ","zip":"85007"}],"phones":["(602) 452-3396"],"urls":["https://www.azcourts.gov/meetthejustices/ViceChiefJusticeRobertMBrutinel.aspx"],"emails":["scclerk@courts.az.gov"],"channels":[{"type":"Facebook","id":"ArizonaSupremeCourt"},{"type":"Twitter","id":"AZCourts"}]},{"name":"Andrew Gould","address":[{"line1":"1501 W. Washington","line2":"Suite 402","city":"Phoenix","state":"AZ","zip":"85007"}],"phones":["(602) 452-3396"],"urls":["https://www.azcourts.gov/meetthejustices/Justice-Andrew-Gould"],"emails":["scclerk@courts.az.gov"],"channels":[{"type":"Facebook","id":"ArizonaSupremeCourt"},{"type":"Twitter","id":"AZCourts"}]},{"name":"John R. Lopez, IV","address":[{"line1":"1501 W. Washington","line2":"Suite 402","city":"Phoenix","state":"AZ","zip":"85007"}],"phones":["(602) 452-3396"],"urls":["https://www.azcourts.gov/meetthejustices/Justice-John-R-Lopez-IV"],"emails":["scclerk@courts.az.gov"],"channels":[{"type":"Facebook","id":"ArizonaSupremeCourt"},{"type":"Twitter","id":"AZCourts"}]},{"name":"Kathy Hoffman","address":[{"line1":"1535 W. Jefferson Street,","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Democratic Party","phones":["(602) 542-3710"],"channels":[{"type":"Facebook","id":"SuperintendentHoffman"},{"type":"Twitter","id":"Supt_Hoffman"}]},{"name":"Boyd Dunn","address":[{"line1":"1200 W. Washington","line2":"2nd Floor","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 542-3935"],"urls":["https://www.azcc.gov/commissioners/BDunn/contact.asp"],"channels":[{"type":"Twitter","id":"corpcommaz"}]},{"name":"Andy Tobin","address":[{"line1":"1200 W. Washington","line2":"2nd Floor","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 542-3625"],"urls":["https://www.azcc.gov/commissioners/ATobin/contact.asp"],"emails":["Tobin-web@azcc.gov"],"channels":[{"type":"Twitter","id":"corpcommaz"}]},{"name":"Bob Burns","address":[{"line1":"1200 W. Washington","line2":"2nd Floor","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 542-3682"],"urls":["https://www.azcc.gov/commissioners/rburns/contact.asp"],"emails":["RBurns-web@azcc.gov"],"channels":[{"type":"Twitter","id":"corpcommaz"}]},{"name":"Justin Olson","address":[{"line1":"1200 W. Washington","line2":"2nd Floor","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Republican Party","phones":["(602) 542-0745"],"urls":["https://www.azcc.gov/commissioners/JOlson/contact.asp"],"emails":["Olson-web@azcc.gov"],"channels":[{"type":"Twitter","id":"corpcommaz"}]},{"name":"Sandra Kennedy","address":[{"line1":"1200 W. Washington","line2":"2nd Floor","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Democratic Party","phones":["(602) 542-3933"],"urls":["https://www.azcc.gov/commissioners/SKennedy/contact.asp"],"emails":["SDKennedy-Web@azcc.gov"],"channels":[{"type":"Twitter","id":"corpcommaz"}]},{"name":"Katie Hobbs","address":[{"line1":"1700 W. Washington Street,","city":"Phoenix","state":"AZ","zip":"85007"}],"party":"Democratic Party","phones":["(602) 542-4285"],"urls":["https://azsos.gov/"],"channels":[{"type":"Facebook","id":"SecretaryHobbs"},{"type":"Twitter","id":"SecretaryHobbs"}]},{"name":"Michael McCord","address":[{"line1":"P.O. Box 729","city":"Florence","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(520) 509-3555"],"urls":["http://www.pinalcountyaz.gov/Treasurer/Pages/Treasurer.aspx"],"emails":["CustomerService@pinalcountyaz.gov"]},{"name":"Kent Volkmer","address":[{"line1":"P.O. Box 887","line2":"Florenze","city":"","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(520) 866-6271"],"urls":["http://www.pinalcountyaz.gov/CountyAttorney/Pages/AboutCountyAttorney.aspx"],"emails":["pinalcountyattorney@pinalcountyaz.gov"],"channels":[{"type":"Facebook","id":"299885690037948"}]},{"name":"Steven J. Fuller","address":[{"line1":"P.O. Box 2028","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5576"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/pages/Judge-Steven-Fuller.aspx"],"emails":["tfelix@courts.az.gov"]},{"name":"Robert C. Olson","address":[{"line1":"P.O. Box 907","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5572"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/pages/Judge-Robert-Olson.aspx"],"emails":["nortega@courts.az.gov"]},{"name":"Patrick K. Gard","address":[{"line1":"P.O. Box 1320","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5405"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/Pages/Judge-Patrick-Gard.aspx"],"emails":["nbrokaw@courts.az.gov"]},{"name":"Kevin D. White","address":[{"line1":"P.O. Box 2578","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5425"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/pages/Judge-Kevin-White.aspx"],"emails":["JHancock@courts.az.gov"]},{"name":"Jason R. Holmberg","address":[{"line1":"P.O. Box 986","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5407"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/pages/Judge-Jason-Holmberg.aspx"],"emails":["bmahlberg@courts.az.gov"]},{"name":"Joseph R. Georgini","address":[{"line1":"P.O. Box 2547","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5497"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/pages/Judge-Joseph-Georgini.aspx"],"emails":["cdrost@courts.az.gov"]},{"name":"Delia R. Neal","address":[{"line1":"PO Box 1583","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5435"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/Pages/Judge-Delia-Neal.aspx"],"emails":["shecook@courts.az.gov"]},{"name":"Daniel A. Washburn","address":[{"line1":"P.O. Box 847","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5409"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/pages/Judge-Daniel-Washburn.aspx"],"emails":["KSherwood@courts.az.gov"]},{"name":"Christopher J. O’Neil","address":[{"line1":"P.O. Box 847","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5479"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/Pages/Judge-Christopher-ONeil.aspx"],"emails":["CWalker@courts.az.gov"]},{"name":"Stephen F. McCarville","address":[{"line1":"P.O. Box 828","city":"Florence","state":"AZ","zip":"85132"}],"party":"Nonpartisan","phones":["(520) 866-5417"],"urls":["http://www.pinalcountyaz.gov/Judicial/Judges/pages/Judge-Stephen-McCarville.aspx"],"emails":["rpadilla@courts.az.gov"]},{"name":"Amanda Stanford","address":[{"line1":"P.O. Box 2730","city":"Florence","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(520) 866-5300"],"urls":["http://www.coscpinalcountyaz.gov/index.html"],"channels":[{"type":"Facebook","id":"pinalcountyclerk"},{"type":"Twitter","id":"AmandaCOSC"}]},{"name":"Jill Broussard","address":[{"line1":"75 N. Bailey Street,","city":"Florence","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(520) 866-6565"],"urls":["http://www.pinalcountyaz.gov/SchoolSuperintendent/Pages/home.aspx"],"emails":["jill.broussard@pinalcountyaz.gov"]},{"name":"Douglas Wolf","address":[{"line1":"P.O. Box 709","city":"Florence","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(520) 866-6361"],"urls":["http://www.pinalcountyaz.gov/Assessor/Pages/Assessor.aspx"],"emails":["assessor@pinalcountyaz.gov"]},{"name":"Virginia Ross","address":[{"line1":"31 N. Pinal Street,","city":"Florence","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(520) 866-6830"],"urls":["http://www.pinalcountyaz.gov/Recorder/Pages/Recorder.aspx"],"emails":["recorder@pinalcountyaz.gov"]},{"name":"Mark Lamb","address":[{"line1":"971 Jason Lopez Circle,","city":"Florence","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(800) 420-8689"],"urls":["http://www.pinalcountyaz.gov/Sheriff/Pages/home.aspx"],"channels":[{"type":"Facebook","id":"151161388286780"},{"type":"Twitter","id":"PinalCSO"}]},{"name":"Anthony Smith","address":[{"line1":"P.O. Box 827","city":"Florence","state":"AZ","zip":"85132"}],"party":"Republican Party","phones":["(520) 866-3960"],"urls":["http://pinalcountyaz.gov/bos/Pages/District4Supervisor.aspx"],"photoUrl":"http://www.pinalcountyaz.gov/bos/PublishingImages/Anthony-Smith-LG.gif","emails":["Anthony.Smith@pinalcountyaz.gov"]}]
答案 0 :(得分:0)
这是一个简短的示例,说明如何从返回的0
0
0
0
0
0
result
$(document).ready(function() {
$("#officials").click(function(){
$.getJSON("https://www.googleapis.com/civicinfo/v2/representatives?address=40790+W+Wade+Dr%2C+Maricopa%2C+AZ+85138%2C+USA&key=AIzaSyBwA2-va1J2oaO3IhPn2xqItnyUyhkfkqk", function(result){
$.each(result.officials , function(k , v){
console.log(v.name +' - '+ v.address[0].line1);
});
});
});
});
注意,您可以从我的代码开始创建自己的循环功能
个人说明::使用jquery时,我更喜欢使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="officials">Get Data</button>
而不是.each()
循环