【问题标题】:I want to convert attributes to elements in Strong-SOAP xmlToJson我想将属性转换为 Strong-SOAP xmlToJson 中的元素
【发布时间】:2017-05-18 09:17:00
【问题描述】:

我正在使用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 属性吗?

【问题讨论】:

    标签: json node.js strong-soap


    【解决方案1】:

    我用 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;
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2012-05-30
      • 1970-01-01
      相关资源
      最近更新 更多