2013-10-04 61 views
0

我有我想要的解析4「整理」只有結果如下JSON回覆:字符串轉換/解析返回正確的價值觀

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」)?

謝謝!

+0

吧? if(item ===「collat​​ion」)return item; 但是你的JSON無效,它應該總是{「key」:「value」}並且你有{「key」,「key」} – Martin

+0

@Martin:這隻會返回4x「整理」 – user2843661

回答

0

如果你有訪問,修改JSON是關鍵:值,否則使用該解決方案:

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

我不能訪問JSON。然而,我發現,當我做了以下檢查:if(item === request.term)return;只要request.term中只有一個單詞,我就可以解決問題。如何在seachbox中輸入幾個詞時解決這個問題? – user2843661