我inventory.json
在服务器上有这样的 json 库存:
[ { "body" : "SUV",
"color" : { "ext" : "White diamond pearl",
"int" : "Taupe"
},
"id" : "276181",
"make" : "Acura",
"miles" : 35949,
"model" : "RDX",
"pic" : [ { "full" : "http://images1.dealercp.com/90961/000JNBD/001_0292.jpg" } ],
"power" : { "drive" : "Front wheel drive",
"eng" : "2.3L DOHC PGM-FI 16-VALVE",
"trans" : "Automatic"
},
"price" : { "net" : 29488 },
"stock" : "6942",
"trim" : "AWD 4dr Tech Pkg SUV",
"vin" : "5J8TB2H53BA000334",
"year" : 2011
},
{ "body" : "Sedan",
"color" : { "ext" : "Premium white pearl",
"int" : "Taupe"
},
"id" : "275622",
"make" : "Acura",
"miles" : 40923,
"model" : "TSX",
"pic" : [ { "full" : "http://images1.dealercp.com/90961/000JMC6/001_1765.jpg" } ],
"power" : { "drive" : "Front wheel drive",
"eng" : "2.4L L4 MPI DOHC 16V",
"trans" : "Automatic"
},
"price" : { "net" : 22288 },
"stock" : "6945",
"trim" : "4dr Sdn I4 Auto Sedan",
"vin" : "JH4CU2F66AC011933",
"year" : 2010
} ]
这里有两个索引,几乎有这样的5000
索引。我这样解析这个json:
var url = "inventory/inventory.json";
$.getJSON(url, function(data){
$.each(data, function(index, item){ //straight-forward loop
if(item.year == 2012) {
$('#desc').append(item.make + ' ' + item.model + ' ' + '<br/>' + item.price.net + '<br/>' + item.pic[0].full);
}
});
});
这工作正常。但问题是,这个搜索和获取过程有点慢,因为已经有 5000 个索引并且它每天都在增加。看起来,解析数据和正常的brute-force method
.
现在我想知道是否有任何时间有效的方法来更快地解析。任何更快的方法来解析而不是直接循环?