分析完整的 JSON 和搜索与值匹配的键
parse complete json and search keys matching a value
我想在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"
}
}
请向我建议实现这一目标的简单方法。
你在自己的答案中使用的递归方法是一个很好的通用解决方案,总是有效的。
但它完全遍历源对象,因此在有大量嵌套子对象的情况下,它可能会影响性能。
使用下面的解决方案,整个源对象也被完全遍历,但工作是由 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]));
}
这是它,并附有解释性评论:
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;
});
我为此做了一个函数:
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]);
}
}
};
如果有人能提供更好的答案,那么请发布它。 :)
您可以使用 for 循环
var results = [];
for (var key in valueableData)
{
if ( key.indexOf("mohit") == 0 )
results.push(valueableData[key]);
}
结果数组将包含匹配的对象。
相关文章:
- 在禁用ng的情况下搜索JSON对象(AngularJS)
- 使用ng类(AngularJS)搜索JSON对象
- 自动完成功能,用于按属性搜索 json 对象
- 如何搜索 json 结构
- 如何在jQuery中搜索JSON
- 搜索 JSON 数组
- 根据变量搜索 JSON 数组
- 递归搜索 JSON 并删除某些子对象
- 使用jQuery提高搜索JSON对象的性能
- 将Multiple Var输入函数,搜索JSON数组,并将Multiple Array Val作为输出接收
- 用于搜索JSON数据的自定义筛选器
- 在JavaScript中搜索Json
- 如何在JavaScript中搜索JSON数组中的特定参数
- 使用jQuery循环浏览twitter搜索JSON
- 搜索JSON以查找特定值
- TypeAhead和Bloodhound只搜索JSON数组中的第一个单词
- 使用JQuery搜索JSON对象
- 使用从下拉菜单输入的用户日期搜索JSON
- 在ember.js中搜索JSON数据
- 二进制搜索JSON对象