如何搜索JSON并查找每个特定名称(带增量)

时间:2017-05-09 14:25:45

标签: javascript jquery json loops

我有一个JSON数据,我想自动查找每个特定名称的出现。

{
  "servergenre": "Classic Rock",
  "servergenre2": "pop",
  "servergenre3": "rock",
  "servergenre4": "jazz",
  "servergenre5": "80s",
  "serverurl": "http://www.name.com",
  "servertitle": "ServerHero",
  "bitrate": "48",
  "samplerate": "0"
}

在这里servergenre以增量(2,3,4,5 and ...)重复几次,因为我不知道JSON中会有多少servergenre,我需要一个循环的方法它可以找到与servergenre的实例一样多的次数,并可能在数组中添加结果。

以下代码:

var URL = "http://name.com/file.json"
$.getJSON(URL, function(data) {
  var i = 1;
  $.each(data.servergenre + i, function(index, value) {
        /// CODE
  });
});

显然上面的代码不起作用,但那是我最初的想法。

那么更好的想法是让它工作并将servergenre的所有现有实例保存在数组中吗?

感谢提前。

4 个答案:

答案 0 :(得分:4)

测试JSON的每个密钥以查看它是否与“servergenre”匹配。如果是,请将相应的值推送到数组。

let json = {
  "servergenre": "Classic Rock",
  "servergenre2": "pop",
  "servergenre3": "rock",
  "servergenre4": "jazz",
  "servergenre5": "80s",
  "serverurl": "http://www.name.com",
  "servertitle": "ServerHero",
  "bitrate": "48",
  "samplerate": "0"
}

let result = []

Object.keys(json).forEach( key => {
  if(/servergenre/.test(key)) result.push(json[key])
})

console.log(result) // ["Classic Rock","pop", "rock","jazz","80s"]

使用过滤器和地图的替代方法(受osynligsebastian的回答启发):

   let json = {
      "servergenre": "Classic Rock",
      "servergenre2": "pop",
      "servergenre3": "rock",
      "servergenre4": "jazz",
      "servergenre5": "80s",
      "serverurl": "http://www.name.com",
      "servertitle": "ServerHero",
      "bitrate": "48",
      "samplerate": "0"
    }

    let result = Object.keys(json)
                    .filter( key => /servergenre/.test(key))
                    .map( key => json[key] )

    console.log(result) // ["Classic Rock","pop", "rock","jazz","80s"]

答案 1 :(得分:2)

过滤包含“servergenre”一词的所有密钥。见this fiddle

var data = {
  "servergenre": "Classic Rock",
  "servergenre2": "pop",
  "servergenre3": "rock",
  "servergenre4": "jazz",
  "servergenre5": "80s",
  "serverurl": "http://www.name.com",
  "servertitle": "ServerHero",
  "bitrate": "48",
  "samplerate": "0"
}

let keys = Object.keys(data).filter(key => key.indexOf('servergenre') > -1);

console.log(keys);

keys.forEach(key => {
  console.log(`${key}: ${data[key]}`);
});

答案 2 :(得分:2)

步骤:

  1. 使用Object.keys()方法获取属性数组。
  2. Object.keys()上使用Array.filter()方法过滤掉具有相同出现次数(substring)的数组中的元素。
  3. 使用string.includes()方法检查字符串是否包含" servergenre"。
  4. 使用Array.length获取元素的计数。
  5. <强>样本

    &#13;
    &#13;
    var jsonObj = {
      "servergenre": "Classic Rock",
      "servergenre2": "pop",
      "servergenre3": "rock",
      "servergenre4": "jazz",
      "servergenre5": "80s",
      "serverurl": "http://www.name.com",
      "servertitle": "ServerHero",
      "bitrate": "48",
      "samplerate": "0"
    };
    
    var res = Object.keys(jsonObj).filter(item => { return item.includes("servergenre"); });
    
    console.log(res.length);
    &#13;
    &#13;
    &#13;

    根据Jeremy Thille评论更新了答案。在下面的演示中,我们使用indexOf ()代替includes()

    &#13;
    &#13;
    var jsonObj = {
      "servergenre": "Classic Rock",
      "servergenre2": "pop",
      "servergenre3": "rock",
      "servergenre4": "jazz",
      "servergenre5": "80s",
      "serverurl": "http://www.name.com",
      "servertitle": "ServerHero",
      "bitrate": "48",
      "samplerate": "0"
    };
    
    var res = Object.keys(jsonObj).filter(item => { return item.indexOf("servergenre") != -1 });
    
    console.log(res.length);
    &#13;
    &#13;
    &#13;

答案 3 :(得分:1)

   var URL = "http://name.com/file.json"
    $.getJSON(URL, function(data) {
      for(var n in data)
      {
        if(n.substr(0,11)=="servergenre") alert(n);
      }
    });