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

时间:2022-02-13 02:05:26

I have the following JSON reply where I want to parse the 4 "collation" results only:

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

where I get the following results:

我得到以下结果:

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

with

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

QUESTION: How can I ONLY get these 4 results:

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

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

without the two (wrong) input results ("restaurants" + "berl")?

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

Thank you!

1 个解决方案

#1


0  

If you have access, modify the JSON to be key: value, otherwise use this solution:

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

$.map(data.spellcheck.suggestions, function (item, i) {
            if (typeof item != "string") return;
            if (item === "collation"){
                return data.spellcheck.suggestions[i+1];
            }
        })

#1


0  

If you have access, modify the JSON to be key: value, otherwise use this solution:

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

$.map(data.spellcheck.suggestions, function (item, i) {
            if (typeof item != "string") return;
            if (item === "collation"){
                return data.spellcheck.suggestions[i+1];
            }
        })