这是ajax请求的结果:
console.log(locations.output);
/*
{
"predictions" : [
{
"description" : "Tehran Province, Iran",
"id" : "4c45c924b261e2bb262360fb5a25d4b9a94088a7",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJf5Us9YQBjj8R0OohvHQms1U",
"reference" : "CjQtAAAA6j_9DtMr30DQ9h8dzemfD2NaWEWBgf5yFlQ92XW4EZvHXqZ8Ob6wGkedetO1xNiZEhCGeIk7jBXCQP_ad4V0R-YeGhSIcGtxcxPh-G4e2DMT97Be4xdfkg",
"structured_formatting" : {
"main_text" : "Tehran Province",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Iran"
},
"terms" : [
{
"offset" : 0,
"value" : "Tehran Province"
},
{
"offset" : 17,
"value" : "Iran"
}
],
"types" : [ "administrative_area_level_1", "political", "geocode" ]
},
{
"description" : "Tehran, Tehran Province, Iran",
"id" : "14788d031b564588682273730e2e16d2617d3548",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJ2dzzH0kAjj8RvCRwVnxps_A",
"reference" : "CkQ1AAAAGXsNCFct3XK9JukMD-C7D7j1f0b-Rk0uBZFULZ9TKziZVKz2HWeNWZ5xWU-aNIceYEioJRxHmYS_O7DhMW32YBIQzGS_zTt6aijJx_onRU9DTRoU-cPuoOF16KrzlFY_4EGlwVf1Izk",
"structured_formatting" : {
"main_text" : "Tehran",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Tehran Province, Iran"
},
"terms" : [
{
"offset" : 0,
"value" : "Tehran"
},
{
"offset" : 8,
"value" : "Tehran Province"
},
{
"offset" : 25,
"value" : "Iran"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Tehranpars, Tehran, Tehran Province, Iran",
"id" : "09a979a1ac286c1ff2ee94c5d36258910d7f6ec6",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJX5PM9hwdjj8R6zn_mu5suoE",
"reference" : "ClRBAAAACKbtLWiAm5kV0JzYtPOLhQTuMxlMMIXxPGRmuLUF7xuAlx7Mueo91OuJmB4mTwo3T0NhnNhamVDUsKFE0fNffkZi1Bv-ZQs1TcXLNx195C0SELOTSsOMn6pYeSnFMtvVqsIaFCxWipFk_wIxDKVoLi8Cd8g993cG",
"structured_formatting" : {
"main_text" : "Tehranpars",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Tehran, Tehran Province, Iran"
},
"terms" : [
{
"offset" : 0,
"value" : "Tehranpars"
},
{
"offset" : 12,
"value" : "Tehran"
},
{
"offset" : 20,
"value" : "Tehran Province"
},
{
"offset" : 37,
"value" : "Iran"
}
],
"types" : [ "neighborhood", "political", "geocode" ]
},
{
"description" : "Tehran Now, Tehran, Tehran Province, Iran",
"id" : "1f7583f258e7e92f03b818ba10c48e20d2bd9f59",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJ61k7RLICjj8RnvjuZpbZeEM",
"reference" : "ClRBAAAAydlnsS8Fjw9ibBXXX-XS4xuY4-7yEqMSZNTz66Q-4hRNnGdEGyOICqFbm4VQ4-ZV7O8fftoznes6-u-6sZyvz8Gp3N3hPLASFvtoWeJBPS8SEE8pPnxD20v8_NPnWLANDG8aFPfquA7RBNHvpEgiH5Q0eIqmtl5k",
"structured_formatting" : {
"main_text" : "Tehran Now",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Tehran, Tehran Province, Iran"
},
"terms" : [
{
"offset" : 0,
"value" : "Tehran Now"
},
{
"offset" : 12,
"value" : "Tehran"
},
{
"offset" : 20,
"value" : "Tehran Province"
},
{
"offset" : 37,
"value" : "Iran"
}
],
"types" : [ "sublocality_level_1", "sublocality", "political", "geocode" ]
},
{
"description" : "Tehran Bazaar, Tehran, Tehran Province, Iran",
"id" : "7a97ba4c05dcb1b11c2153bd39673b6737aa5748",
"matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"place_id" : "ChIJJX5my-8Bjj8RfAUG71zqw8s",
"reference" : "ClREAAAABs4mvt65BUJ7Z2arVA_cv7V0tdPJKoVy010IOFrlS-L6VfhrEmIQAuxTXVQhnAA19fjEtVbPag3lfb-UrsmnMROnzvXiffA7MKUvgASMwvwSEGx-aGYRXyP1Q9Bxr7HmMVIaFKaxQEb8tg4L7IlRIbITWpG4F5Mf",
"structured_formatting" : {
"main_text" : "Tehran Bazaar",
"main_text_matched_substrings" : [
{
"length" : 6,
"offset" : 0
}
],
"secondary_text" : "Tehran, Tehran Province, Iran"
},
"terms" : [
{
"offset" : 0,
"value" : "Tehran Bazaar"
},
{
"offset" : 15,
"value" : "Tehran"
},
{
"offset" : 23,
"value" : "Tehran Province"
},
{
"offset" : 40,
"value" : "Iran"
}
],
"types" : [ "neighborhood", "political", "geocode" ]
}
],
"status" : "OK"
}
现在,我需要列出结果的自动完成列表。我的意思是我需要做这样的事情:
<ul>
<li>Tehran Province, Iran</li>
<li>Tehran, Tehran Province, Iran</li>
<li>Tehranpars, Tehran, Tehran Province, Iran</li>
<li>Tehran Bazaar, Tehran, Tehran Province, Iran</li>
</ul>
但是我不知道如何选择结果的值description
。这是我的代码:
var html = "<ul>";
$.each(locations.output, function (index, value) {
console.log(value);
})
var html += "</ul>";
/*
"
p
r
e
d
.
.
.
如您所见,结果是很多单个字符。我如何获得这些价值?
为了从响应中获取JavaScript对象,可以使用JSON.parse()。此方法解析一个JSON字符串,构造该字符串描述的JavaScript值或对象:
var response = JSON.parse(locations.output);
比您可以response
像这样在代码中使用:
// Mocking response variable for code example..
var response = {"predictions": [{"description": "Tehran Province, Iran","id": "4c45c924b261e2bb262360fb5a25d4b9a94088a7","matched_substrings": [{"length": 6,"offset": 0}],"place_id": "ChIJf5Us9YQBjj8R0OohvHQms1U","reference": "CjQtAAAA6j_9DtMr30DQ9h8dzemfD2NaWEWBgf5yFlQ92XW4EZvHXqZ8Ob6wGkedetO1xNiZEhCGeIk7jBXCQP_ad4V0R-YeGhSIcGtxcxPh-G4e2DMT97Be4xdfkg","structured_formatting": {"main_text": "Tehran Province","main_text_matched_substrings": [{"length": 6,"offset": 0}],"secondary_text": "Iran"},"terms": [{"offset": 0,"value": "Tehran Province"},{"offset": 17,"value": "Iran"}],"types": ["administrative_area_level_1", "political", "geocode"]},{"description": "Tehran, Tehran Province, Iran","id": "14788d031b564588682273730e2e16d2617d3548","matched_substrings": [{"length": 6,"offset": 0}],"place_id": "ChIJ2dzzH0kAjj8RvCRwVnxps_A","reference": "CkQ1AAAAGXsNCFct3XK9JukMD-C7D7j1f0b-Rk0uBZFULZ9TKziZVKz2HWeNWZ5xWU-aNIceYEioJRxHmYS_O7DhMW32YBIQzGS_zTt6aijJx_onRU9DTRoU-cPuoOF16KrzlFY_4EGlwVf1Izk","structured_formatting": {"main_text": "Tehran","main_text_matched_substrings": [{"length": 6,"offset": 0}],"secondary_text": "Tehran Province, Iran"},"terms": [{"offset": 0,"value": "Tehran"},{"offset": 8,"value": "Tehran Province"},{"offset": 25,"value": "Iran"}],"types": ["locality", "political", "geocode"]},{"description": "Tehranpars, Tehran, Tehran Province, Iran","id": "09a979a1ac286c1ff2ee94c5d36258910d7f6ec6","matched_substrings": [{"length": 6,"offset": 0}],"place_id": "ChIJX5PM9hwdjj8R6zn_mu5suoE","reference": "ClRBAAAACKbtLWiAm5kV0JzYtPOLhQTuMxlMMIXxPGRmuLUF7xuAlx7Mueo91OuJmB4mTwo3T0NhnNhamVDUsKFE0fNffkZi1Bv-ZQs1TcXLNx195C0SELOTSsOMn6pYeSnFMtvVqsIaFCxWipFk_wIxDKVoLi8Cd8g993cG","structured_formatting": {"main_text": "Tehranpars","main_text_matched_substrings": [{"length": 6,"offset": 0}],"secondary_text": "Tehran, Tehran Province, Iran"},"terms": [{"offset": 0,"value": "Tehranpars"},{"offset": 12,"value": "Tehran"},{"offset": 20,"value": "Tehran Province"},{"offset": 37,"value": "Iran"}],"types": ["neighborhood", "political", "geocode"]},{"description": "Tehran Now, Tehran, Tehran Province, Iran","id": "1f7583f258e7e92f03b818ba10c48e20d2bd9f59","matched_substrings": [{"length": 6,"offset": 0}],"place_id": "ChIJ61k7RLICjj8RnvjuZpbZeEM","reference": "ClRBAAAAydlnsS8Fjw9ibBXXX-XS4xuY4-7yEqMSZNTz66Q-4hRNnGdEGyOICqFbm4VQ4-ZV7O8fftoznes6-u-6sZyvz8Gp3N3hPLASFvtoWeJBPS8SEE8pPnxD20v8_NPnWLANDG8aFPfquA7RBNHvpEgiH5Q0eIqmtl5k","structured_formatting": {"main_text": "Tehran Now","main_text_matched_substrings": [{"length": 6,"offset": 0}],"secondary_text": "Tehran, Tehran Province, Iran"},"terms": [{"offset": 0,"value": "Tehran Now"},{"offset": 12,"value": "Tehran"},{"offset": 20,"value": "Tehran Province"},{"offset": 37,"value": "Iran"}],"types": ["sublocality_level_1", "sublocality", "political", "geocode"]},{"description": "Tehran Bazaar, Tehran, Tehran Province, Iran","id": "7a97ba4c05dcb1b11c2153bd39673b6737aa5748","matched_substrings": [{"length": 6,"offset": 0}],"place_id": "ChIJJX5my-8Bjj8RfAUG71zqw8s","reference": "ClREAAAABs4mvt65BUJ7Z2arVA_cv7V0tdPJKoVy010IOFrlS-L6VfhrEmIQAuxTXVQhnAA19fjEtVbPag3lfb-UrsmnMROnzvXiffA7MKUvgASMwvwSEGx-aGYRXyP1Q9Bxr7HmMVIaFKaxQEb8tg4L7IlRIbITWpG4F5Mf","structured_formatting": {"main_text": "Tehran Bazaar","main_text_matched_substrings": [{"length": 6,"offset": 0}],"secondary_text": "Tehran, Tehran Province, Iran"},"terms": [{"offset": 0,"value": "Tehran Bazaar"},{"offset": 15,"value": "Tehran"},{"offset": 23,"value": "Tehran Province"},{"offset": 40,"value": "Iran"}],"types": ["neighborhood", "political", "geocode"]}], "status": "OK"};
var html = '<ul>';
$.each(response.predictions, function (index, value) {
html += '<li>' + value.description + '</li>';
});
html += '</ul>';
$('#output').html(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句