0

我正在使用strong-soap node.js 模块将 XML 字符串转换为 JSON 字符串:xmlHandler.xmlToJson()

但是 SOAP 中的 XML 具有标签属性,它们从strong-soap转换为 JSON 元素,其关键 $attributes 如下:

{"fuelStation":{
"$attributes":{"id":"62611","lastUpdate":"2017-05-17T19:01:14.745Z","provider":"mdm"},
"location": ...

如何删除此 $attributes 键以获得 JSON 字符串,例如:

{"fuelStation":{
    "id":"62611","lastUpdate":"2017-05-17T19:01:14.745Z","provider":"mdm",
    "location": ...

我只能删除整个 $attributes 但这不是我的目标:

const root = xmlHandler.xmlToJson(null, xmlString, null);
const jsonString = JSON.stringify(root.Body.GetFuelStationsResponse, function replacer(key, value) {
                    return key !== '$attributes' ? value: undefined;
                });

我可以使用 strong-soap xmlToJson 或 JSON.stringify 替换函数将属性转换为 JSON 属性吗?

4

1 回答 1

0

我用 JSON.stringify 修复了它:

const jsonString = JSON.stringify(root.Body.GetFuelStationsResponse, function replacer(key, value) {
                if(value !== null && typeof value === 'object' && '$attributes' in value){
                    for(const k in value.$attributes) value[k]=value.$attributes[k];
                    value.$attributes = undefined;
                }
                return value;
            });
于 2017-05-18T13:26:38.107 回答