Здесь мы имеем JSON дерево:Как искать JSON дерево JavaScript
{
"title":"Recipe Puppy",
"version":0.1,
"href":"http:\/\/www.recipepuppy.com\/",
"results":[
{
"title":"Ginger Champagne",
"href":"http:\/\/allrecipes.com\/Recipe\/Ginger-Champagne\/Detail.aspx",
"ingredients":"champagne, ginger, ice, vodka",
"thumbnail":"http:\/\/img.recipepuppy.com\/1.jpg"
},
{
"title":"Potato and Cheese Frittata",
"href":"http:\/\/allrecipes.com\/Recipe\/Potato-and-Cheese-Frittata\/Detail.aspx",
"ingredients":"cheddar cheese, eggs, olive oil, onions, potato, salt",
"thumbnail":"http:\/\/img.recipepuppy.com\/2.jpg"
},
{
"title":"Eggnog Thumbprints",
"href":"http:\/\/allrecipes.com\/Recipe\/Eggnog-Thumbprints\/Detail.aspx",
"ingredients":"ginger, brown sugar, butter, butter, powdered sugar, eggs, flour, nutmeg, rum, salt, vanilla extract, sugar",
"thumbnail":"http:\/\/img.recipepuppy.com\/3.jpg"
}
]
}
Мне нужно сделать поиск через него, поэтому я получить title
(рецепта), если искомое слово в ingredients
является например, "имбирь"
Вот что я пытался сделать:
var url = requestUrl("http://www.recipepuppy.com/api/");
var data = JSON.parse(url);
var ourResults = [];
var searchField = "ingredients";
var searchVal = "onions";
for (var i=0 ; i < data.results.length ; i++)
{
if (data.results[i][searchField] == searchVal) {
ourResultss.push(data.results[i]);
}
Спасибо!
Вы можете быть заинтересованы в моем JSON.search API, который в 5 раз быстрее, чем $ .grep и позволяет осуществлять поиск с использованием регулярных выражений. См. Http://json.spiritway.co/ – mgwhitfield