У меня есть следующий формат JSON. Мне нужно получить доступ к тексту в поле "Смотрите", которое присутствует в поле "выделение". Пожалуйста, дайте мне знать, как мне получить доступ к нему с помощью jquery. Помощь приветствуется.
{
"responseHeader": {
"status": 0,
"QTime": 61,
"params": {
"df": "See",
"indent": "true",
"q": "boating\n",
"hl.simple.pre": "<em>",
"hl.simple.post": "</em>",
"wt": "json",
"hl": "true"
}
},
"response": {
"numFound": 28,
"start": 0,
"docs": [{
"id": "26204",
"title": "Osoyoos",
"Getin": "\nGet in\n\n\nOsoyoos is in the south central interior of British Columbia, approximately 400nbsp;km east of Vancouver at the junction of Highways 97 and 3 near the border of Washington State. ",
"Other": "ootenays.\n\n\n\nWikipedia\nOsoyoos, British Columbia\n\n\n\n\n",
"Understand": "asant weather in April. Current weather conditions and historical climate data can be found online at Environment Canada . \n\n",
"See": "boating and fishing. Lakeside campsites and privacy make this a popular camping area. Reservations are necessary during the summer months, call\n1-800-689-9025.\n\n",
"_version_": 1453197586830721000
}]
},
"highlighting": {
"112855": {
"See": [
"<em>Boating</em>, Swimming, Fishing - <em>Boating</em>, swimming, Fishing in Melamadai Vandiyur Lake provides some"]
}
}
}
предположим, что вы декодируете строку JSON, вы отправили в переменную jsonDecodedObj
jsonDecodedObj = $.parseJSON( jsonEncodedString )
Вы можете получить доступ к Смотрите этот путь
see = jsonDecodedObj['highlighting']['112855']['See'][0];
Обратите внимание, что "See" - это массив, содержащий только один объект (таким образом, final [0]).
Но в случае, если ключ "112855" (который предположительно является идентификатором выделенного элемента) может быть изменен от ответа на ответ, который вы можете зафиксировать. См. Следующий шаг
jsonDecodedObj = $.parseJSON( jsonEncodedString )
highlighting = jsonDecodedObj.highlighting;
for( obj in highlighting ) {
See = highlighting[obj].See[0];
break;
}
Я не уверен, но это сработает
var data = JSON.stringify({
"responseHeader": {
"status": 0,
"QTime": 61,
"params": {
"df": "See",
"indent": "true",
"q": "boating\n",
"hl.simple.pre": "<em>",
"hl.simple.post": "</em>",
"wt": "json",
"hl": "true"
}
},
"response": {
"numFound": 28,
"start": 0,
"docs": [{
"id": "26204",
"title": "Osoyoos",
"Getin": "\nGet in\n\n\nOsoyoos is in the south central interior of British Columbia, approximately 400nbsp;km east of Vancouver at the junction of Highways 97 and 3 near the border of Washington State. ",
"Other": "ootenays.\n\n\n\nWikipedia\nOsoyoos, British Columbia\n\n\n\n\n",
"Understand": "asant weather in April. Current weather conditions and historical climate data can be found online at Environment Canada . \n\n",
"See": "boating and fishing. Lakeside campsites and privacy make this a popular camping area. Reservations are necessary during the summer months, call\n1-800-689-9025.\n\n",
"_version_": 1453197586830721000
}]
},
"highlighting": {
"112855": {
"See": [
"<em>Boating</em>, Swimming, Fishing - <em>Boating</em>, swimming, Fishing in Melamadai Vandiyur Lake provides some"]
}
}
})
var parse = JSON.parse(data)
console.log(parse.highlighting[112855].See )
Дает этот результат
["<em>Boating</em>, Swimming, Fishing - <em>Boating</em>, swimming, Fishing in Melamadai Vandiyur Lake provides some"]
Подготовьте имя переменной:
highlighting[112855].See
YourVariable.highlighting.112855.See
, вотSee
массив. Вы можете использовать индекс для доступа к его элементу