解析匹配值的完整json和搜索键

时间:2016-03-12 02:59:57

标签: javascript json algorithm recursion data-structures

我想在javascript中遍历整个JSON并找到启动特定键的值。 假设这是我的JSON数据:

var data = {
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "abc":{
      "mohit_ab": {
        "id1":"i am id 1",
        "abcd":{
          "a" :"b",
          "c":"d"
        },
        "who" : "who are you",
        "id2":"i am id 2"
      },
      "mohit_231abcd": {
        "id1":"i am id 3",
        "identity" : "i am real mohit",
        "id2":"i am id 4"
      }
    },
    "hji":{
      "mohit_adss231": {
        "id1":"i am id 5",
        "identity" : "i am mohit in real",
        "id2":"i am id 6"
      },
      "mohit": {
        "id7":"i am id 7",
        "identity" : "i am mohit and you?",
        "id8":"i am id 8"
      }
    },
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
};

我想在javascript中解析完整的JSON并获取其键以" mohit"开头的值。例如以上输入的输出是:

valueableData:{
  "mohit_ab": {
        "id1":"i am id 1",
        "abcd":{
          "a" :"b",
          "c":"d"
        },
        "who" : "who are you",
        "id2":"i am id 2"
      },
      "mohit_231abcd": {
        "id1":"i am id 3",
        "identity" : "i am real mohit",
        "id2":"i am id 4"
      },
      "mohit_adss231": {
        "id1":"i am id 5",
        "identity" : "i am mohit in real",
        "id2":"i am id 6"
      },
      "mohit": {
        "id7":"i am id 7",
        "identity" : "i am mohit and you?",
        "id8":"i am id 8"
      }
}

请建议我实现这一目标的简便方法。

3 个答案:

答案 0 :(得分:2)

您在自己的答案中使用的递归方法是一个很好的通用解决方案,始终具有功能性 但是它完全遍历了源对象,因此如果存在大量嵌套的子对象,它可能会影响性能。

通过下面的解决方案,整个源对象也可以完全遍历,但是工作由JS内部进行,速度更快。
更重要的是,代码更轻巧,更易读。



var data = {
  "array": [
    1,
    2,
    3
  ],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
    "a": "b",
    "abc":{
      "mohit_ab": {
        "id1":"i am id 1",
        "abcd":{
          "a" :"b",
          "c":"d"
        },
        "who" : "who are you",
        "id2":"i am id 2"
      },
      "mohit_231abcd": {
        "id1":"i am id 3",
        "identity" : "i am real mohit",
        "id2":"i am id 4"
      }
    },
    "hji":{
      "mohit_adss231": {
        "id1":"i am id 5",
        "identity" : "i am mohit in real",
        "id2":"i am id 6"
      },
      "mohit": {
        "id7":"i am id 7",
        "identity" : "i am mohit and you?",
        "id8":"i am id 8"
      }
    },
    "c": "d",
    "e": "f"
  },
  "string": "Hello World"
};

var valuableData = {};
JSON.parse(JSON.stringify(data), function(key, value) {
  if (key.substr(0, 5) == 'mohit') {
    valuableData[key] = value;
  }
  return value;
});

document.write('ValuableData:');
for (var key in valuableData) {
  document.write('<br />' + key + ': ' + JSON.stringify(valuableData[key]));
}
&#13;
&#13;
&#13;

就是这样,带有解释性评论:

var valuableData = {};
// use JSON.parse, not to get its result but only to benefit of its "reviver" function
JSON.parse(JSON.stringify(data), function(key, value) {
  // when encoutering one of the keys we're looking for, populate our own result
  if (key.substr(0, 5) == 'mohit') {
    valuableData[key] = value;
  }
  // in any case, don't break the normal generation of the JSON.parse result
  // (or the keys we select above would have been left empty!)
  return value;
});

答案 1 :(得分:1)

我为此做了一个功能:

var valueAbleData = {};
  this.add = function(value) {
      for (var key in value){
            if(key.indexOf("mohit") > -1){
                valueAbleData[key] =  value[key];
            }
            else if(value[key] !== null && typeof value[key] === 'object'){
                this.add(value[key]);
            }
            }
    };

如果有人能提供更好的答案,请发布。 :)

答案 2 :(得分:0)

您可以使用 for循环

var results = [];
for (var key in valueableData)
{
    if ( key.indexOf("mohit") == 0 )
      results.push(valueableData[key]);
}

结果数组将包含匹配的对象。