在以下情况下如何获取 JSON 对象:
这是我的帖子请求:
var articles = client.post("http://host:port/path", args, function (data, response) {
// console.log(xml.parseBuffer(data))
console.log(data.toString('utf8'));
// return xml.parseBuffer(data);
});
data.toString('utf8') 的结果如下:
<WebServiceResponse>
<page>
...
</page>
<articleDataList>
<articleId>
<idArticle>100000</idArticle>
<index>test</index>
</articleId>
<goodType>test</goodType>
<idAttributeSubject>100001</idAttributeSubject>
<identyfiable>false</identyfiable>
<isActive>true</isActive>
<isGoodSet>false</isGoodSet>
<longName>test</longName>
<translationSubjectId>
<idTranslationSubject>100408</idTranslationSubject>
</translationSubjectId>
<unitCode>szt</unitCode>
<vatRate>0.2300</vatRate>
</articleDataList>
<articleDataList>
...
</articleDataList>
<articleDataList>
...
和 xml.parseBuffer(data) 看起来: { name: 'articleDataList', childs: [Object] },
我需要放入只有以下内容的文章对象:idArticle、index 和shortName 有什么简单的方法吗?
最佳答案
在此输出中:
{ name: 'articleDataList', childs: [Object] }
Object
表示需要字符串化的 JavaScript 对象,如下所示:JSON.stringify(Object)
或者你应该先将整个 Buffer 转换为字符串,然后再转换为 JSON 或 javascript 对象:
xml.parseString(data.toString('utf8'))
关于node.js - 如何获取 json 休息响应 - node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41386282/