字符串转换/解析以返回正确的值

时间:2013-10-04 14:44:32

标签: jquery xml ajax json jsonp

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

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

谢谢!

1 个答案:

答案 0 :(得分: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];
            }
        })