我正在努力将xml转换为json对象,然后从转换后的对象中提取节点。我正在使用busyboy从服务器上传的文件中读取。之后我使用inspect将xml转换为json,然后打印json对象。最终输出似乎是
{ 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'和content ='Adnan Ali'的对象。
打印命令是
console.log(inspect(order, {colors: true, depth: Infinity}));
答案 0 :(得分:1)
您应该能够使用此路径content: 'Adnan Ali'
data.root.children[1].children[0]
与const data = {
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'
}]
}]
}
};
console.log(data.root.children[1].children[0])
对象:
data
说明:
root
是一个包含root
对象的对象。 children
是一个包含root.children
数组的对象。 1
(索引children
)中的第二个元素是一个对象,它包含另一个0
数组,其中包含您在第一个索引(chgrp www-data file.php
chmod 770 file.php
)处查找的对象。
答案 1 :(得分:1)
由于您正在使用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; - ))
来源:
https://www.npmjs.com/package/JSONPath
http://goessner.net/articles/JsonPath/
答案 2 :(得分:1)
您需要在对象数组中搜索您感兴趣的对象。有多种方法可以做到这一点,包括Array.prototype.find
(不确定它是否在所有Node.js版本中都可用)和{{ 3}}
使用Array.prototype.filter
解决方案看起来像这样(未经测试):
function findObject(array, key, value) {
var filtered = array.filter(obj => (obj[key] === value));
if (filtered.length !== 1) throw new Error('Found ' + filtered.length + ' objects with `' + key + '`=`' + value + '`, expected to find 1.');
return filtered[0];
}
var shipto = findObject(input.root.children, 'name', 'shipto');
var name = findObject(shipto.children, 'name', 'name').content;
console.log(name);
答案 3 :(得分:0)
请考虑使用object-scan。将头包裹起来,它会非常强大。
const objectScan = require('object-scan');
const find = (input) => objectScan(['**'], {
abort: true,
rtn: 'value',
filterFn: ({ value }) => value.content === 'Adnan Ali' && value.name === 'name'
})(input);
const 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"}]}]}};
console.log(find(tobj));
// => { name: 'name', attributes: {}, children: [], content: 'Adnan Ali' }