从json对象中提取子对象
我正在努力将xml转换为json对象,然后从转换后的对象中提取节点。我使用busyboy读取上传到服务器上的文件。之后,我使用inspect将xml转换为json,然后打印json对象。最终输出结果似乎如下所示从json对象中提取子对象,json,node.js,xml,Json,Node.js,Xml,我正在努力将xml转换为json对象,然后从转换后的对象中提取节点。我使用busyboy读取上传到服务器上的文件。之后,我使用inspect将xml转换为json,然后打印json对象。最终输出结果似乎如下所示 { declaration: { attributes: { version: '1.0', encoding: 'utf-8' } }, root: { name: 'order', attributes: { orderid: '123456',
{ declaration: { attributes: { version: '1.0', encoding: 'utf-8' } },
root:
{ name: 'order',
attributes:
{ orderid: '123456',
xmlns: 'http://www.someRandomNameSpace.com' },
children:
[ { name: 'orderperson',
attributes: {},
children: [],
content: 'str1234' },
{ name: 'shipto',
attributes: {},
children:
[ { name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali' },
我想从这个对象中读取'name'='Adnan Ali',在nodejs中如何实现?我的意思是,我怎样才能到达一个有name='name'和content='Adnan Ali'的对象
打印命令是
console.log(检查(顺序,{colors:true,depth:Infinity})
您应该能够通过此路径data.root.children[1]。children[0]
访问内容为“Adnan Ali”的对象:
const数据={
声明:{
属性:{
版本:“1.0”,
编码:“utf-8”
}
},
根目录:{
名称:'订单',
属性:{
医嘱ID:'123456',
xmlns:'http://www.someRandomNameSpace.com'
},
儿童:[{
姓名:'orderperson',
属性:{},
儿童:[],
内容:“str1234”
}, {
名称:'shipto',
属性:{},
儿童:[{
姓名:'姓名',
属性:{},
儿童:[],
内容:“阿德南·阿里”
}]
}]
}
};
console.log(data.root.children[1].children[0])
您应该能够通过此路径data.root.children[1].children[0]
访问具有内容的对象:“Adnan Ali'
:
const数据={
声明:{
属性:{
版本:“1.0”,
编码:“utf-8”
}
},
根目录:{
名称:'订单',
属性:{
医嘱ID:'123456',
xmlns:'http://www.someRandomNameSpace.com'
},
儿童:[{
姓名:'orderperson',
属性:{},
儿童:[],
内容:“str1234”
}, {
名称:'shipto',
属性:{},
儿童:[{
姓名:'姓名',
属性:{},
儿童:[],
内容:“阿德南·阿里”
}]
}]
}
};
console.log(data.root.children[1].children[0])
既然您正在使用NodeJS,那么尝试一下JSONPath可能是个好主意。然后你可以这样做:
var jp = require("JSONPath");
var tobj = { "declaration": { "attributes": { "version": '1.0', "encoding": 'utf-8' } },
"root":
{ "name": 'order',
"attributes":
{ "orderid": '123456',
"xmlns": 'http://www.someRandomNameSpace.com' },
"children":
[ { "name": 'orderperson',
"attributes": {},
"children": [],
"content": 'str1234' },
{ "name": 'shipto',
"attributes": {},
"children":
[ { "name": 'name',
"attributes": {},
"children": [],
"content": 'Adnan Ali'
}
]
}
]
}};
var result = jp.eval(tobj, "$..children[?(@.name === 'name' && @.content === 'Adnan Ali')]");
console.log(result);
示例输出:
[ { name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali' } ]
(不要忘记安装JSONPath;-))
资料来源:既然您使用的是NodeJS,那么试试JSONPath可能是个好主意。然后你可以这样做:
var jp = require("JSONPath");
var tobj = { "declaration": { "attributes": { "version": '1.0', "encoding": 'utf-8' } },
"root":
{ "name": 'order',
"attributes":
{ "orderid": '123456',
"xmlns": 'http://www.someRandomNameSpace.com' },
"children":
[ { "name": 'orderperson',
"attributes": {},
"children": [],
"content": 'str1234' },
{ "name": 'shipto',
"attributes": {},
"children":
[ { "name": 'name',
"attributes": {},
"children": [],
"content": 'Adnan Ali'
}
]
}
]
}};
var result = jp.eval(tobj, "$..children[?(@.name === 'name' && @.content === 'Adnan Ali')]");
console.log(result);
示例输出:
[ { name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali' } ]
(不要忘记安装JSONPath;-))
资料来源:您需要在对象数组中搜索您感兴趣的对象。有多种方法可以做到这一点,包括(不确定它是否在所有Node.js版本中都可用)和 使用
Array.prototype.filter
解决方案可能如下所示(未经测试):
您需要在对象数组中搜索感兴趣的对象。有多种方法可以做到这一点,包括(不确定它是否在所有Node.js版本中都可用)和 使用
Array.prototype.filter
解决方案可能如下所示(未经测试):
考虑使用。只要你把头绕在它身上,它就会非常强大
//const objectScan=require('object-scan');
常量find=(输入)=>objectScan(['**']{
中止:对,
rtn:'值',
filterFn:({value})=>value.content=='Adnan Ali'&&value.name=='name'
})(输入);
const tobj={声明:{属性:{版本:'1.0',编码:'utf-8'}}},根:{name:'order',属性:{orderid:'123456',xmlns:'http://www.someRandomNameSpace.com},子项:[{name:'orderperson',属性:{},子项:[],内容:'str1234'},{name:'shipto',属性:{},子项:[{name:'name',属性:{},子项:[],内容:'Adnan Ali'}]}}};
console.log(find(tobj));
//=>{name:'name',属性:{},子项:[],内容:'Adnan Ali'}
。作为控制台包装{最大高度:100%!重要;顶部:0}
考虑使用。只要你把头绕在它身上,它就会非常强大
//const objectScan=require('object-scan');
常量find=(输入)=>objectScan(['**']{
中止:对,
rtn:'值',
filterFn:({value})=>value.content=='Adnan Ali'&&value.name=='name'
})(输入);
const tobj={声明:{属性:{版本:'1.0',编码:'utf-8'}}},根:{name:'order',属性:{orderid:'123456',xmlns:'http://www.someRandomNameSpace.com},子项:[{name:'orderperson',属性:{},子项:[],内容:'str1234'},{name:'shipto',属性:{},子项:[{name:'name',属性:{},子项:[],内容:'Adnan Ali'}]}}};
console.log(find(tobj));
//=>{name:'name',属性:{},子项:[],内容:'Adnan Ali'}
。作为控制台包装{最大高度:100%!重要;顶部:0}
这就是我对生成错误感兴趣的原因。因为它在filtered.length和'objects with…'之间缺少+号,所以修复了它,并且它工作了。谢谢这是答案中“未测试”的部分我更正了它。这就是我对生成错误感兴趣的原因。因为它在filtered.length和'objects with…'之间缺少+号,所以修复了它,并且它工作了。谢谢这是答案中“未测试”的部分我纠正了它。