json字符串解析 - json文件解析



肥皂节点是解析JSON到XML错误 (1)

在Nodejs中使用Soap模块(通过loopback-connector-soap),我试图将我的soap请求转换成json到xml,但是我有一些问题...

这是什么:

<ns:UserDefinedFields>
    <ns:UserDefinedField>
        <ns:displayName>Campaign?</ns:displayName>
        <ns:fieldValue>Yes</ns:fieldValue>
    </ns:UserDefinedField>
</ns:UserDefinedFields>
<ns:UserDefinedFields>
    <ns:UserDefinedField>
        <ns:displayName>Anticipated Use</ns:displayName>
    <ns:fieldValue>6</ns:fieldValue>
</ns:UserDefinedField>

它应该是什么:

<ns:UserDefinedFields>
    <ns:UserDefinedField>
        <base:displayName>Campaign?</base:displayName>
        <base:fieldValue>Yes</base:fieldValue>
    </ns:UserDefinedField>
    <ns:UserDefinedField>
        <base:displayName>Anticipated Use</base:displayName>
        <base:fieldValue>6</base:fieldValue>
    </ns:UserDefinedField>
</ns:UserDefinedFields>
  • 应该只有一个<ns:UserDefinedFields>
  • 而且应该是<base:fieldValue>而不是<ns:fieldValue>

任何建议将是超级有益的! 谢谢!

XML:

{
    ...
    "UserDefinedFields": [
        {
            "displayName": "Campaign?",
            "fieldValue": "Yes"
        },
        {
            "displayName": "Anticipated Use",
            "fieldValue": 6
        }
    ]
    ...
}

XSD:

...
<xs:element name="UserDefinedFields" minOccurs="0">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="UserDefinedField" type="base:UserDefinedFieldType" minOccurs="0" maxOccurs="15"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
...

要处理命名空间,请传递ignoredNamespaces中的soap.createClient选项。

{
    "ignoredNamespaces": {
        namespaces: ['ns']
    },
}

然后手动添加名称空间。 这是丑陋的,但它的作品。

要处理多个UserDefinedFields ,它应该看起来像这样:

{
    . . .
    DomainRegistration['ns:UserDefinedFields']['ns:UserDefinedField'][0] : {
        "base:displayName": "Campaign?",
        "base:fieldValue": "Yes"
    },
    DomainRegistration['ns:UserDefinedFields']['ns:UserDefinedField'][1] : {
        "base:displayName": "Anticipated Use",
        "base:fieldValue": 6
    }
    . . .
}

或者更简单地说:

{
    ouside: {
        insideA: [
            'one',
            'two',
        ]
        insideB: [
            {'one':'ONE'},
            {'two':'TWO'}
        ]
    }
}

//will create:

<outside>
    <insideA>
        <one />
        <two />
    </insideA>
    <insideB>
        <one>ONE</one>
    </insideB>
    <insideB>
        <two>TWO</two>
    </insideB>
</outside>




loopbackjs