3

我正在尝试编写一些代码来解析和搜索行业的 JSON 文件。该代码将被添加到一个自动完成脚本并返回他们找到的项目(与这个问题无关)。我有以下 JSON 文件:

{
  "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');
    }
  });
});

但是,它不起作用。

我尝试了不同的变体:

if (item.name.search(new RegExp(q, "i")) != -1) {

这个抛出一个错误Uncaught TypeError: Cannot call method 'search' of undefined

有任何想法吗?


编辑: 感谢下面的@Arun P Johny,我解决了这些问题。我的 JSON 文件存在问题:{}每个industrysubsectorsector. 我需要遍历每个subsectorand sector

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');
                }
            })
        });
    });
});
4

2 回答 2

3

您需要遍历sectors,subsectorsindustries数组

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');
                }
            })
        });
    });
});
于 2013-11-13T08:49:41.817 回答
1

您需要遍历sectors数组,因此each代码应为:

$.each(data.sectors, function(i, item) {
    if (item.subsectors.industries.search(new RegExp(q, "i")) != -1) {
        $('<li />').html(item.name).appendTo('body');
    }
});

此外,您的item.name价值将是未定义的,因为您需要访问item.subsectors[0].industries[0].name. 后面的属性[0]是数组,如果需要从中检索所有值,则需要循环遍历它们。[0]只会为您提供第一个值。

于 2013-11-13T08:47:37.407 回答