jQuery中的wierd-json错误

wierd json error in jQuery

本文关键字:错误 wierd-json 中的 jQuery      更新时间:2023-09-26

我有以下json:

{
    "responseHeader": {
        "status": 0,
        "QTime": 1
    },
    "spellcheck": {
        "suggestions": [
            "a",
            {
                "numFound": 4,
                "startOffset": 0,
                "endOffset": 1,
                "suggestion": [
                    "api",
                    "and",
                    "as",
                    "an"
                ]
            },
            "collation",
            "api"
        ]
    }
}

我想访问"建议"数组,为此我在jQuery:中使用了它

$.getJSON('http://localhost:8983/solr/suggest/?q=' + query + '&wt=json&json.wrf=?', {
})
    .done(function(response){
        // just for testing
        alert(response.spellcheck.suggestions.suggestion); // error: response.spellcheck is not defined
    });

"response.spellcheck"的值显示为未定义,而"response.responseHeader"显示[object object],我也可以访问responseHead下的元素。但我不知道"拼写检查"有什么问题。帮助

suggestions.suggestions无法工作。CCD_ 2是一个数组。您需要使用[]运算符对数组进行索引,以获得特定的建议。

具体来说,根据您发布的JSON,您需要suggestions[1].suggestion

使用console.log打印响应,然后可以相应地进行

正确的拼写是response.spellcheck.suggestions(您已经使用了suggesstions),这是一个数组,所以您可以使用索引查看其上下文。。。例如:

alert(response.spellcheck.suggestions.suggestion[0]); // "a"

所以你需要:

response.spellcheck.suggestions.suggestion[1].suggestion

JS Fiddle示例。