0

我有以下 JSON 回复,我只想解析 4 个“整理”结果:

jQuery191029421305245357143_1380819227858(
{
    "responseHeader": {
        "status": 0,
        "QTime": 127
    },
    "command": "build",
    "spellcheck": {
        "suggestions": [
            "restaurant",
            {
                "numFound": 1,
                "startOffset": 0,
                "endOffset": 10,
                "suggestion": [
                    "restaurants"
                ]
            },
            "berl",
            {
                "numFound": 4,
                "startOffset": 11,
                "endOffset": 15,
                "suggestion": [
                    "berlin",
                    "berlin brandenburg",
                    "berlin hamburg",
                    "berliner"
                ]
            },
            "collation",
            "restaurant berlin",
            "collation",
            "restaurant (berlin brandenburg)",
            "collation",
            "restaurants berlin",
            "collation",
            "restaurant (berlin hamburg)"
        ]
    }
}
)

我得到以下结果:

"restaurant"
"berl"
"restaurant berlin"
"restaurant (berlin brandenburg)"
"restaurants berlin"
"restaurant (berlin hamburg)"

success: function( data ) {
response( $.map(data.spellcheck.suggestions, function(item) {    
    if (typeof item != "string") return;
    if (item === "collation") return;
    return item;
}));

问题:我怎样才能只得到这 4 个结果:

"restaurant berlin"
"restaurant (berlin brandenburg)"
"restaurants berlin"
"restaurant (berlin hamburg)"

没有两个(错误的)输入结果(“restaurants”+“berl”)?

谢谢!

4

1 回答 1

0

如果您有访问权限,请将 JSON 修改为 key: value,否则使用此解决方案:

$.map(data.spellcheck.suggestions, function (item, i) {
            if (typeof item != "string") return;
            if (item === "collation"){
                return data.spellcheck.suggestions[i+1];
            }
        })
于 2013-10-04T15:04:35.987 回答