我正在从api接收xml数据,请检查这个链接https://developers.cj.com/docs/rest-apis/advertiser-lookup。
为了正确使用xml,我需要将xml解析为json或js对象。
我尝试过xml2js,它以如下方式转换内容。
<?xml version="1.0" encoding="UTF-8"?>
<cj-api><error-message> You must provide an Authorization header.</error-message></cj-api>{
'cj-api': { 'error-message': [ 'You must provide an Authorization header.' ] }
}在这种情况下,我不需要数组,只需要简单的值。有我可以使用的工作解析器吗?
发布于 2022-03-25 01:00:59
您可以像这样使用camaro
const { transform } = require('camaro')
async function main() {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<cj-api><error-message> You must provide an Authorization header.</error-message></cj-api>
`
const template = {
errorMessage: '/cj-api/error-message'
}
const output = await transform(xml, template)
console.log(output.message);
}
main()输出
You must provide an Authorization header.https://stackoverflow.com/questions/71609118
复制相似问题