gpt4 book ai didi

json - Node 上的 SOAP 将 json 解析为 xml 错误

转载 作者:IT老高 更新时间:2023-10-28 23:06:32 26 4
gpt4 key购买 nike

使用 Nodejs 中的 Soap 模块(通过 loopback-connector-soap),我正在尝试将 json 转换为 xml 以用于我的 soap 请求,但我有一些问题......

它是什么:

<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>
...

最佳答案

要处理命名空间,请将soap.createClient 选项传递给ignoreNamespaces。

{
"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>

关于json - Node 上的 SOAP 将 json 解析为 xml 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31034705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com