访问JSON中嵌套数组的字段

时间:2013-12-01 15:47:15

标签: jquery json

我有以下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"]
        }
    }
}

3 个答案:

答案 0 :(得分:0)

假设你解码了你发布到变量 jsonDecodedObj

的JSON字符串
jsonDecodedObj = $.parseJSON( jsonEncodedString )

您可以以这种方式访问​​

see = jsonDecodedObj['highlighting']['112855']['See'][0];

请注意,“See”是一个只包含一个对象的数组(因此是最终的[0])。

如果密钥'112855'(可能是突出显示的项目的ID)可能会从响应更改为响应,您可以捕获参见跟进方式

jsonDecodedObj = $.parseJSON( jsonEncodedString )

highlighting = jsonDecodedObj.highlighting;

for( obj in highlighting ) {
    See = highlighting[obj].See[0];
    break;
}

答案 1 :(得分:0)

使用变量名称前缀:

highlighting[112855].See

答案 2 :(得分:0)

我不确定但是这会有效

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"]