在JavaScript中搜索Json
searching through Json in JavaScript
我有一个Json,里面有文件夹和文件的层次结构我正在尝试通过文件夹Id获取文件夹及其子文件夹中的文件数这是json
var jsonStr = {
"hierarchy": {
"date": "2014/09/24 15:21:23",
"folder": {
"name": "Root",
"id": "Root",
"file": [{
"id": "22U2621210__PIN_検査報告書Ver1.0_20140923162232.xls"
}, {
"id": "C22-1(EU仕様)_20140923162409.xlsx"
}, {
"id": "Machine_Inspection_20140923162329.xlsx"
}],
"folder": {
"name": "Level-1",
"id": "1411396172645",
"file": {
"id": "22U2621210__PIN_検査報告書Ver1.0_20140923162232.xls"
},
"folder": {
"name": "123",
"id": "1411538469568",
"file": [{
"id": "C22-1(EU仕様)_20140923162409.xlsx"
}, {
"id": "Machine_Inspection_20140923162329.xlsx"
}]
}
}
}
}
};
如果要通过搜索特定文件夹及其子文件夹中的id来获取文件数,则所有文件夹都带有名称和id例如,如果我放文件夹name "123"
和id "1411538469568"
,它应该只给我两个文件,即"C22-1(EU仕様)_20140923162409.xlsx"
和"Machine_Inspection_20140923162329.xlsx"
,但如果我放folder name "Root"
和d= "Root"
,它应该返回所有文件的id
这是我正在处理的Fiddlehttp://jsfiddle.net/ma3kno2o/
您可以使用Defiant.js
下面是一个Fiddle,用于您的具体搜索场景,以提取ID为root、名称为root:的元素的文件ID。我在这个例子中使用了Defiant.js:
http://jsfiddle.net/3z8mqr3u/1/
Defiant.js在@Cheery的答案中优于自定义搜索,要获得文件的ID,我必须使用一行代码:
var ids = JSON.search(json, "//*[name = 'Root' and id = 'Root']/file/id");
它在搜索动态数据时不太容易出错。Defiant.js使用XPath表达式。查看此链接了解更多信息:
http://defiantjs.com/
以下是一些其他选项:
你可以使用普通的JQuery
如何使用jQuery 搜索JSON树
您可以使用JsonPath。这就像JSON文件的XPath。你可以做这样的事情:
$..folder.file
http://goessner.net/articles/JsonPath/
https://code.google.com/p/jsonpath/
https://github.com/s3u/JSONPath
您可以使用Json Query。它有自己的深度查询语言。例如:
var数据={grouped_public:{"好友":[{name:"Steve",country:"NZ"},{名称:"Bob",国家/地区:"US"}],"敌人":[{名称:"邪恶的史蒂夫",国家:"AU"}]}}
jsonQuery('grouped_people[][country=NZ]', {data: data})
https://github.com/mmckegg/json-query
如果你不喜欢这里的任何一个,你可以找到更多的选择:JSON有查询语言吗?
不是最好的(抱歉,凌晨4点)解决方案,而是直接通过递归的方法。。在正常情况下,您的结构不支持相同级别的文件夹,所以我重新配置了它,并提供了相应的代码:http://jsfiddle.net/ma3kno2o/5/
function getFiles(id)
{
var files = searchFolders(jsonStr.hierarchy.folders, false);
alert('Found ' + files.length + " files'n" + JSON.stringify(files));
function searchFolders(tree, count_files)
{
var data = [];
$.each(tree, function(key, val) {
var into = !count_files ? val.id == id : count_files;
if (val.files && into)
$.merge(data, getFiles(val.files));
if (val.folders)
$.merge(data, searchFolders(val.folders, into));
});
return data;
}
function getFiles(tree)
{
var files = [];
if (tree.id) return [tree.id];
$.each(tree, function(key,val) {
if (val.id)
files.push(val.id);
});
return files;
};
}
var jsonStr = {
"hierarchy": {
"date": "2014/09/24 15:21:23",
"folders": [{
"name": "Root",
"id": "Root",
"files": [{
"id": "file.1"
}, {
"id": "file.2"
}, {
"id": "file.3"
}],
"folders": [{
"name": "Level-1",
"id": "1411396172645",
"files": {
"id": "file.4"
},
"folders": [{
"name": "123",
"id": "1411538469568",
"files": [{
"id": "file.5"
}, {
"id": "file.6"
}]},
{
"name": "123",
"id": "1411538469569",
"files": [{
"id": "file.7"
}, {
"id": "file.8"
}]
}]
}]
}]
}
};
旧的代码不起作用,所以我为你的新变种重写了它
http://jsfiddle.net/ma3kno2o/8/
function getFiles(id)
{
var stp = -1;
var files = searchFolders(jsonStr.hierarchy, false);
alert('Found ' + files.length + " files'n" + JSON.stringify(files));
function searchFolders(tree, count_files)
{
var data = [];
var folders = tree.folder.length > 1 ? tree.folder : [tree.folder];
$.each(folders, function(key, val) {
var into = !count_files ? val.id == id : count_files;
if (val.file && into)
$.merge(data, getFiles(val.file));
if (val.folder)
$.merge(data, searchFolders(val, into));
});
return data;
}
function getFiles(tree)
{
var files = [];
if (tree.id) return [tree.id];
$.each(tree, function(key,val) {
if (val.id)
files.push(val.id);
});
return files;
};
}
var jsonStr= {"hierarchy":{"date":"2014/09/24 18:13:00","folder":{"name":"Root","id":"Root","file":[{"id":"file.1"},{"id":"file.2"},{"id":"file.3"}],"folder":[{"name":"Level-1","id":"1411396172645","file":{"id":"file.4"},"folder":{"name":"123","id":"1411538469568","file":[{"id":"file.5"},{"id":"file.6"}],"folder":{"name":"123-a","id":"1411549962260","file":{"id":"file.7"}}}},{"name":"level-2","id":"1411549976987","file":{"id":"file.8"}}]}}};
- 如何使用jQuery按键搜索和获取json行数据
- JSON和jQuery实时搜索
- 使用javascript或angularjs特定过滤器搜索字符串中第一个img标记的json值
- 在禁用ng的情况下搜索JSON对象(AngularJS)
- 我需要迭代一个JSON数组——不知道如何做到——已经搜索过了,但仍然可以'我一点也不知道
- 搜索包含值的json对象键,然后取消设置
- (临时)在Web应用程序中存储JSON搜索结果
- 代码点火器:搜索自动完成 JSON 解析意外字符
- 使用节点在 JSON 数组中搜索项目(最好不迭代)
- 将图像添加到 json 搜索栏
- 在搜索应用程序中用JSON数据交换JavaScript数组
- JSON - 搜索具有变量名称(未知)的键
- 使用JQuery UI自动完成从JSON搜索数据不起作用
- 如何在键入时应用ajax/json搜索过滤器
- 用localStorage缓存每个JSON搜索查询
- 使用Javascript和JSON搜索,使用表单参数
- 通过JSON搜索多个关键字(包含regex)
- 在Angular中编辑JSON搜索结果
- 选择2 4.0 JSON搜索不起作用
- 将URL固定到即时JSON搜索脚本的SERP