Javascript 使用jQuery解析和搜索JSON文件
我试图编写一些代码来解析和搜索行业的JSON文件。代码将添加到自动完成脚本中,并返回找到的项目(与此问题无关)。我有以下JSON文件:Javascript 使用jQuery解析和搜索JSON文件,javascript,jquery,json,parsing,search,Javascript,Jquery,Json,Parsing,Search,我试图编写一些代码来解析和搜索行业的JSON文件。代码将添加到自动完成脚本中,并返回找到的项目(与此问题无关)。我有以下JSON文件: { "sectors": [ { "title": "Business, Finance & Technology", "subsectors": [ { "title": "Finance and insurance", "industries": [
{
"sectors": [
{
"title": "Business, Finance & Technology",
"subsectors": [
{
"title": "Finance and insurance",
"industries": [
{
"name": "Retail banking",
"name": "Insurance",
"name": "Investment banking"
}
],
"title": "Business Services",
"industries": [
{
"name": "Accounting & Audit",
"name": "Recruitment",
"name": "Legal services"
}
]
}
],
"title": "Life & Consumer",
"subsectors": [
{
"title": "Life Sciences",
"industries": [
{
"name": "Biotechnology",
"name": "Pharmaceutical",
"name": "Medical supplies"
}
],
"title": "Healthcare",
"industries": [
{
"name": "Surgery",
"name": "Medicine",
"name": "Nursery"
}
]
}
]
}
]
}
该代码:
var q = 'Insurance';
$.getJSON('models/industries.json', function(data) {
$.each(data, function(i, item) {
if (item.sectors.subsectors.industries.search(new RegExp(q, "i")) != -1) {
$('<li />').html(item.name).appendTo('body');
}
});
});
这一个抛出一个错误uncaughttypeerror:无法调用未定义的方法“search”
有什么想法吗
编辑: 感谢下面的@Arun P Johny,我解决了这些问题。我的JSON文件存在问题:每个
行业
、子部门
和部门
都需要大括号{}
。我需要迭代每个子部门
和部门
:
var q = 'Insurance',
regex = new RegExp(q, "i");
$.getJSON('models/industries.json', function (data) {
$.each(data.sectors, function (i, sector) {
$.each(sector.subsectors, function (i, subsector) {
$.each(subsector.industries, function (i, industry) {
if (industry.name.search(regex) != -1) {
$('<li />').html(industry.name).appendTo('body');
}
})
});
});
});
var q='保险',
正则表达式=新正则表达式(q,“i”);
$.getJSON('models/industries.json',函数(数据){
$.each(数据.扇区,函数(i,扇区){
$。每个(部门、子部门、职能部门(i、子部门){
$。每个(子部门、行业、职能(i、行业){
if(industry.name.search(regex)!=-1){
$('').html(industry.name).appendTo('body');
}
})
});
});
});
您需要在扇区
数组上循环,因此每个
代码应该是:
$.each(data.sectors, function(i, item) {
if (item.subsectors.industries.search(new RegExp(q, "i")) != -1) {
$('<li />').html(item.name).appendTo('body');
}
});
$。每个(数据、扇区、功能(i、项){
if(item.subsectors.industries.search(new RegExp(q,“i”)))!=-1){
$('').html(item.name).appendTo('body');
}
});
此外,您的
项.name
值将未定义,因为您需要访问项.subsectors[0].industries[0].name
。后面带有[0]
的属性是数组,如果需要从中检索所有值,则需要对其进行循环<代码>[0]将只获取第一个值。您需要迭代部门
、子部门
和行业
数组
var q = 'Insurance',
regex = new RegExp(q, "i");
$.getJSON('models/industries.json', function (data) {
$.each(data.sectors, function (i, sector) {
$.each(sector.subsectors, function (i, subsector) {
$.each(subsector.industries, function (i, industry) {
if (industry.name.search(regex) != -1) {
$('<li />').html(industry.name).appendTo('body');
}
})
});
});
});
var q='保险',
正则表达式=新正则表达式(q,“i”);
$.getJSON('models/industries.json',函数(数据){
$.each(数据.扇区,函数(i,扇区){
$。每个(部门、子部门、职能部门(i、子部门){
$。每个(子部门、行业、职能(i、行业){
if(industry.name.search(regex)!=-1){
$('').html(industry.name).appendTo('body');
}
})
});
});
});
谢谢,但我仍然收到此错误未捕获类型错误:Object#没有方法“搜索”
@Alex tryindustry.name.search(regex)
错误已消失,但结果未追加。如果中的代码未被执行,则中的代码将被执行。由于某种原因行业。名称将显示托儿所,这是最后一个元素。@Alex您还有一个问题{“名称”:“生物技术”,“名称”:“制药”,“名称”:“医疗用品”}
无效,应该是[{“名称”:“Biotechnology”},{“name”:“Pharmaceutical”},{“name”:“Medical supplies”}]
我看到您正在尝试使用正则表达式。尝试一下,看看这是否更符合您的需要,因为迭代方法很麻烦,比基于字符串的搜索/JSON重建我链接的库慢5倍。
var q = 'Insurance',
regex = new RegExp(q, "i");
$.getJSON('models/industries.json', function (data) {
$.each(data.sectors, function (i, sector) {
$.each(sector.subsectors, function (i, subsector) {
$.each(subsector.industries, function (i, industry) {
if (industry.name.search(regex) != -1) {
$('<li />').html(industry.name).appendTo('body');
}
})
});
});
});