使用jQuery解析和搜索JSON文件

时间:2013-11-13 08:44:51

标签: javascript jquery json parsing search

我正在尝试编写一些代码来解析和搜索行业的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文件存在问题:每个{}industrysubsector都需要花括号sector。我需要遍历每个subsectorsector

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

2 个答案:

答案 0 :(得分:3)

您需要迭代sectorssubsectorsindustries数组

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

答案 1 :(得分: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]只会获得第一个值。