Javascript 在json中搜索比1更深的内容
我正试图找出如何在JSON文件中搜索更深的内容 其结构如下Javascript 在json中搜索比1更深的内容,javascript,jquery,json,Javascript,Jquery,Json,我正试图找出如何在JSON文件中搜索更深的内容 其结构如下 json>data>movies> $('#search').keyup(function () { // i understand this to watch the form id search var searchField = $('#search').val(); // sets variable searchfield from typed content, .val grabs the cont
json>data>movies>
$('#search').keyup(function () { // i understand this to watch the form id search
var searchField = $('#search').val(); // sets variable searchfield from typed content, .val grabs the content
if (searchField.length) // this sets the length to false by default ensuring the code only runs when it is supposed to and not all the time
{
var myExp = new RegExp(searchField, "i"); // sets variable myexp to regular expression using "i" for case insensitive
var found = 0; // sets variable found to 0 to keep div hidden until match is found
$.getJSON('/secure/movies.json', function (data) { //gets movies.json file pushes to data function
var output = '<ul class="searchresults">';
$.each(data, function (key, val) { // this should make key pairs from data
if (val.title.search(myExp) !== -1) { // this is the problem i think
console.log(val); // this should show our search term results in console
found = 1;
output += '<li>';
output += '<h2>' + val.title + '</h2>';
output += "<a href=" + val.image + ' target="_blank" ></a>';
output += '</li>';
}
});
output += '</ul>';
if (found == 1) {
$('#update').removeClass('update-hidden');
$('#update').html(output);
} else {
$('#update').addClass('update-hidden');
}
});
} else {
$('#update').addClass('update-hidden');
}
});
在电影中,不同的电影被列举为0、1、2、3、4等
json>data>movies>3>
将包含第四部电影的电影信息
每个都包含键值对。我正在使用的代码看起来只有一个深度,我似乎无法让它更深入
代码如下
json>data>movies>
$('#search').keyup(function () { // i understand this to watch the form id search
var searchField = $('#search').val(); // sets variable searchfield from typed content, .val grabs the content
if (searchField.length) // this sets the length to false by default ensuring the code only runs when it is supposed to and not all the time
{
var myExp = new RegExp(searchField, "i"); // sets variable myexp to regular expression using "i" for case insensitive
var found = 0; // sets variable found to 0 to keep div hidden until match is found
$.getJSON('/secure/movies.json', function (data) { //gets movies.json file pushes to data function
var output = '<ul class="searchresults">';
$.each(data, function (key, val) { // this should make key pairs from data
if (val.title.search(myExp) !== -1) { // this is the problem i think
console.log(val); // this should show our search term results in console
found = 1;
output += '<li>';
output += '<h2>' + val.title + '</h2>';
output += "<a href=" + val.image + ' target="_blank" ></a>';
output += '</li>';
}
});
output += '</ul>';
if (found == 1) {
$('#update').removeClass('update-hidden');
$('#update').html(output);
} else {
$('#update').addClass('update-hidden');
}
});
} else {
$('#update').addClass('update-hidden');
}
});
我尝试过操纵val.title.searchmyexp,但我可能错过了处理val.title.search的机会。提前感谢console.logval显示了什么?能否显示JSON数据的外观?控制台中没有任何内容没有错误。下面是一段json。太大,无法在这里发布[链接]@morrin