gpt4 book ai didi

javascript - 如何解析 JS 中的字符串,这是 google location api 的结果?

转载 作者:太空宇宙 更新时间:2023-11-04 15:54:08 24 4
gpt4 key购买 nike

这是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>

关于javascript - 如何解析 JS 中的字符串,这是 google location api 的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47310175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com