我使用了一些在线链接将XML格式数据转换为JSON,我得到的结果是
{
"SOAP-ENV:Envelope": {
"-xmlns:SOAP-ENV": "http://schemas.xmlsoap.org/soap/envelope/",
"-xmlns:SOAP-ENC": "http://schemas.xmlsoap.org/soap/encoding/",
"-xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
"-xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"-xmlns:hdr": "urn:PASAHeader",
"-xmlns:ns": "urn:PSFVUC_DMTR",
"-xmlns:ex": "urn:PASAexploitationWS",
"SOAP-ENV:Body": {
"-SOAP-ENV:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/",
"-id": "_0",
"ns:getEleLocTopicDtlResp": {
"tControle": {
"-xsi:type": "ns:tControle",
"iErrorType": {
"-xsi:type": "xsd:int",
"#text": "0"
},
"iRetError": {
"-xsi:type": "xsd:int",
"#text": "0"
},
"sErrorMessage": { "-xsi:type": "xsd:string" }
},
"tGlobalSetting": {
"-xsi:type": "ns:tGlobalSettingTopic",
"iGlobalSettingId": {
"-xsi:type": "xsd:int",
"#text": "10"
},
"sGlobalSettingCode": {
"-xsi:type": "xsd:string",
"#text": "RAJA"
},
"iDisplayMode": {
"-xsi:type": "xsd:int",
"#text": "0"
},
"iOrderVariant": {
"-xsi:type": "xsd:int",
"#text": "0"
}
},
"pListTopic": {
"-xsi:type": "ns:sListELTopic",
"nTopic": {
"-xsi:type": "xsd:int",
"#text": "2"
},
"pTListTopic": {
"-xsi:type": "SOAP-ENC:Array",
"-SOAP-ENC:offset": "[0]",
"-SOAP-ENC:arrayType": "ns:tELTopic[2]",
"item": [
{
"-xsi:type": "ns:tELTopic",
"iTopicId": {
"-xsi:type": "xsd:int",
"#text": "5"
},
"sTopicName": {
"-xsi:type": "xsd:string",
"#text": "TOP1 TESTING PLEASEDONTDELETE"
},
"pListClearCriteria": {
"-xsi:type": "ns:sListClearCriteria",
"nClearCriteria": {
"-xsi:type": "xsd:int",
"#text": "0"
}
},
"pListImage": {
"-xsi:type": "ns:sListImage",
"nImage": {
"-xsi:type": "xsd:int",
"#text": "0"
}
},
"pListTag": {
"-xsi:type": "ns:sListELTag",
"nTag": {
"-xsi:type": "xsd:int",
"#text": "0"
}
}
},
{
"-xsi:type": "ns:tELTopic",
"-SOAP-ENC:position": "[1]",
"iTopicId": {
"-xsi:type": "xsd:int",
"#text": "6"
},
"sTopicName": {
"-xsi:type": "xsd:string",
"#text": "TOP2 TESTING PLEASEDONTDELETE"
},
"pListClearCriteria": {
"-xsi:type": "ns:sListClearCriteria",
"nClearCriteria": {
"-xsi:type": "xsd:int",
"#text": "0"
}
},
"pListImage": {
"-xsi:type": "ns:sListImage",
"nImage": {
"-xsi:type": "xsd:int",
"#text": "0"
}
},
"pListTag": {
"-xsi:type": "ns:sListELTag",
"nTag": {
"-xsi:type": "xsd:int",
"#text": "0"
}
}
}
]
}
}
}
}
}
}
现在我想在JS中动态添加和追加值,但是我从未遇到过如此复杂的JSON结构,我遇到了带有键值对的JSON。
要求是将此JSON(在更改之后)转换为XML并发送到服务器。 我们如何处理这样的JSON结构。使用这样的JSON结构也存在任何问题。 用精确的json更新,我将与之合作。
场景:说我必须附加“pListTopic”项如何管理“-xsi:type”:“SOAP-ENC:Array”
动态地为每个项目我的目标是只是向数组添加一个新元素但我仍然需要维护每个元素的“-xsi:type”:值,我该如何实现这一点。
答案 0 :(得分:1)
这是正常的JSON。您可以通常的方式追加和修改值。例如,此代码
var test={"first":{"first1":"blabla","first2":"blabla2"},"second":"blabla3","third":{"third1":"blabla4","third2":"blabla5"}}
console.log(test)
console.log('-----')
console.log(test.first)
console.log('-----')
console.log(test.first.first1)
console.log('-----')
test.fourth = "just added";
test.third.third2 = "just corrected";
test.third.third3 = "hey it works";
console.log(test)
在屏幕上打印(使用nodejs)以下内容:
{ first: { first1: 'blabla', first2: 'blabla2' },
second: 'blabla3',
third: { third1: 'blabla4', third2: 'blabla5' } }
-----
{ first1: 'blabla', first2: 'blabla2' }
-----
blabla
-----
{ first: { first1: 'blabla', first2: 'blabla2' },
second: 'blabla3',
third:
{ third1: 'blabla4',
third2: 'just corrected',
third3: 'hey it works' },
fourth: 'just added' }
回答你的问题。然而,将该对象转换为JSON仅修改它然后将其更改为XML并没有多大意义。我会修改原始的XML结构。