Javascript 在Json中搜索并获得有限的搜索结果
我有一个JSON文件,看起来像这样Javascript 在Json中搜索并获得有限的搜索结果,javascript,json,Javascript,Json,我有一个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
[{
"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"
}]
我想通过word=“Aa”、track\u 2=“Aa”、track\u 3=“aar”获得前2个或3个结果。。我该怎么做
我需要什么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);
}
});
});
});
有没有办法通过扩展一些代码来获得所需的内容?在这部分添加几行代码-
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;
}
}
有什么理由不将json解析为对象而只是比较属性吗?我认为这一行-
'id':jsonarr[I]。word
应该改为'id':jsonarr[I]。id
我正在使用json文件作为firefoxOS的脱机数据库,这就是为什么我将json解析为对象@Shillyit!但我也想按2-3个条件在json文件中搜索。。有解决方案吗?2-3个条件更多??例子??您正在搜索每个对象的word
属性中的字符串,是否还要搜索type、desc等???很抱歉答复太晚,是。。我想同时使用类型和单词搜索。。。但是我不需要descr搜索..使用相同的东西--if(jsonArrr[i].type.search(matchMe)>-1{…}
。对于和条件(匹配类型和单词)if(jsonarr[i].word.search(matchMe)>-1&&jsonarr[i].type.search(matchMe)>-1{……}