2015-07-28 71 views
1

我有一個JSON文件,它看起來像這樣搜索並獲得有限的搜索結果

[{ 
    "id": "2", 
    "word": "Aam", 
    "type": "n.", 
    "descr": " A Dutch and German measure of liquids, varying in different cities, being at Amsterdam about 41 wine gallons, at Antwerp 36 1\/2, at Hamburg 38 1\/4.", 
    "track": "a", 
    "track_2": "Aa", 
    "track_3": "Aam" 
}, { 
    "id": "3", 
    "word": "Aard-vark", 
    "type": "n.", 
    "descr": " An edentate mammal, of the genus Orycteropus, somewhat resembling a pig, common in some parts of Southern Africa. It burrows in the ground, and feeds entirely on ants, which it catches with its long, slimy tongue.", 
    "track": "a", 
    "track_2": "Aa", 
    "track_3": "Aar" 
}, { 
    "id": "4", 
    "word": "Aard-wolf", 
    "type": "n.", 
    "descr": " A carnivorous quadruped (Proteles Lalandii), of South Africa, resembling the fox and hyena. See Proteles.", 
    "track": "a", 
    "track_2": "Aa", 
    "track_3": "Aar" 
}, { 
    "id": "5", 
    "word": "Aaronic", 
    "type": "a.", 
    "descr": " Alt. of Aaronical", 
    "track": "a", 
    "track_2": "Aa", 
    "track_3": "Aar" 
}, { 
    "id": "6", 
    "word": "Aaronical", 
    "type": "a.", 
    "descr": " Pertaining to Aaron, the first high priest of the Jews.", 
    "track": "a", 
    "track_2": "Aa", 
    "track_3": "Aar" 
}, { 
    "id": "7", 
    "word": "Aarons rod", 
    "type": "", 
    "descr": " A rod with one serpent twined around it, thus differing from the caduceus of Mercury, which has two.", 
    "track": "a", 
    "track_2": "Aa", 
    "track_3": "Aar" 
}, { 
    "id": "8", 
    "word": "Aarons rod", 
    "type": "", 
    "descr": " A plant with a tall flowering stem; esp. the great mullein, or hag-taper, and the golden-rod.", 
    "track": "a", 
    "track_2": "Aa", 
    "track_3": "Aar" 
}] 

我想獲得前2或3的結果的其中一句話=「AA」,track_2 =「AA」, track_3 =「aar」..我該怎麼做?

我需要
1.查詢JSON中通過什麼字符串 2.獲取有限的結果 3.我想2-3的條件

我想要在JSON文件中搜索這

$(window).load(function() { 

    $('#search').keyup(function() { 
     var searchField = $('#search').val(); 

     $.getJSON('files/aa.json', function(data) { 

      var jsonArrr = data; 
      var matchMe = new RegExp('^' + searchField, 'i'); 
      var matches = []; 

      for (var i in jsonArrr) { 
       if (jsonArrr[i].word.search(matchMe) > -1) { 
        matches.push({ 
         'id': jsonArrr[i].word, 
         'word': jsonArrr[i].word 
        }); 
       } 
      } 

      for (var i in matches) { 
       console.log(matches[i].word); 
      } 
     }); 
    }); 
}); 

有沒有辦法讓我需要擴大一些代碼?

+1

你不解析JSON成一個對象,只是比較性質的原因嗎? – Shilly

+1

我認爲這行' - id':jsonArrr [i] .word'應該改爲''id':jsonArrr [i] .id' –

+0

我使用json文件作爲firefoxOS的離線數據庫,這就是爲什麼我'解析JSON到一個對象@Shilly –

回答

1

添加幾行代碼在這個部分 -

var matches = []; 

for (var i in jsonArrr) { 

    if (jsonArrr[i].word.search(matchMe) > -1 && jsonArrr[i].type.search(matchType) > -1) { // define matchType for the type keyword 

      matches.push({'id': jsonArrr[i].word , 'word': jsonArrr[i].word}); 

    } 

if(matches.length == 2) { // replace 2 with your number of results - 1 
break; 
} 

} 
+0

它的工作原理!但我也想在2-3條件下搜索json文件..任何解決方案呢? –

+0

2-3條件更多?例??你正在每個對象的'''屬性中搜索一個字符串,你是否想要搜索類型,desc等? –

+0

抱歉,對於遲到的答覆和是的..我想同時使用類型和單詞進行搜索......但我不需要descr搜索.. –