在我的工作中,我需要将XML转换为JSON,并且同样的JSON转换为XML,因此,我在我的C#代码中使用NewtonSoft.Json( Version = 6.0.0.0 )。
我需要强制单个节点成为一个数组,我正在使用相同的XML结构,就像你在newtonsoft JSON的site中看到的那样。
我正在使用以下代码将JSON转换为XML。
XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(JSON, "", true);
当我将JSON转换为XML时,它会添加一个属性,如 xmlns:json ='http://james.newtonking.com/projects/json'& XML中的 json:Array ='true'。还添加了结果XML。
{"Test_Service" : {"fname":"mark","lname":"joye","CarCompany":"saab","CarNumber":"9741","IsInsured":"true","safty":["ABS","AirBags","childdoorlock"],"CarDescription":"test Car","collections":[{"XYZ":"1","PQR":"11","contactdetails":[{"contname":"DOM","contnumber":"8787"},{"contname":"COM","contnumber":"4564","addtionaldetails":[{"description":"54657667"}]},{"contname":"gf","contnumber":"123","addtionaldetails":[{"description":"123"}]}]}]}}
<?xml version="1.0"?>
<Test_Service>
<fname>mark</fname>
<lname>joye</lname>
<CarCompany>saab</CarCompany>
<CarNumber>9741</CarNumber>
<IsInsured>true</IsInsured>
<safty>ABS</safty>
<safty>AirBags</safty>
<safty>childdoorlock</safty>
<CarDescription>test Car</CarDescription>
<collections
xmlns:json="http://james.newtonking.com/projects/json" json:Array="true">
<XYZ>1</XYZ>
<PQR>11</PQR>
<contactdetails>
<contname>DOM</contname>
<contnumber>8787</contnumber>
</contactdetails>
<contactdetails>
<contname>COM</contname>
<contnumber>4564</contnumber>
<addtionaldetails json:Array="true">
<description>54657667</description>
</addtionaldetails>
</contactdetails>
<contactdetails>
<contname>gf</contname>
<contnumber>123</contnumber>
<addtionaldetails json:Array="true">
<description>123</description>
</addtionaldetails>
</contactdetails>
</collections>
</Test_Service>
但是,如果我使用带有 ns3 标记的JSON(下面提到)并尝试在转换后将JSON转换为XML,则不会添加像 xmlns:json ='{{ 3}}'&amp;转换后的XML中的 json:Array ='true'。转换后的XML将在下面添加。
{"ns3:Test_Service" : {"@xmlns:ns3":"http://www.CCKS.org/XRT/Form","ns3:fname":"mark","ns3:lname":"joye","ns3:CarCompany":"saab","ns3:CarNumber":"9741","ns3:IsInsured":"true","ns3:safty":["ABS","AirBags","childdoorlock"],"ns3:CarDescription":"test Car","ns3:collections":[{"ns3:XYZ":"1","ns3:PQR":"11","ns3:contactdetails":[{"ns3:contname":"DOM","ns3:contnumber":"8787"},{"ns3:contname":"COM","ns3:contnumber":"4564","ns3:addtionaldetails":[{"ns3:description":"54657667"}]},{"ns3:contname":"gf","ns3:contnumber":"123","ns3:addtionaldetails":[{"ns3:description":"123"}]}]}]}}
<?xml version="1.0"?>
<ns3:Test_Service
xmlns:ns3="http://www.CCKS.org/XRT/Form">
<ns3:fname>mark</ns3:fname>
<ns3:lname>joye</ns3:lname>
<ns3:CarCompany>saab</ns3:CarCompany>
<ns3:CarNumber>9741</ns3:CarNumber>
<ns3:IsInsured>true</ns3:IsInsured>
<ns3:safty>ABS</ns3:safty>
<ns3:safty>AirBags</ns3:safty>
<ns3:safty>childdoorlock</ns3:safty>
<ns3:CarDescription>test Car</ns3:CarDescription>
<ns3:collections>
<ns3:XYZ>1</ns3:XYZ>
<ns3:PQR>11</ns3:PQR>
<ns3:contactdetails>
<ns3:contname>DOM</ns3:contname>
<ns3:contnumber>8787</ns3:contnumber>
</ns3:contactdetails>
<ns3:contactdetails>
<ns3:contname>COM</ns3:contname>
<ns3:contnumber>4564</ns3:contnumber>
<ns3:addtionaldetails>
<ns3:description>54657667</ns3:description>
</ns3:addtionaldetails>
</ns3:contactdetails>
<ns3:contactdetails>
<ns3:contname>gf</ns3:contname>
<ns3:contnumber>123</ns3:contnumber>
<ns3:addtionaldetails>
<ns3:description>123</ns3:description>
</ns3:addtionaldetails>
</ns3:contactdetails>
</ns3:collections>
</ns3:Test_Service>
答案 0 :(得分:1)
您正在尝试合并两个错误的XML命名空间,如果从集合标记及其子标记中删除ns3,那么您将看到结果。请参阅附件截图。我对此进行了测试。
以下是您更正的JSON
{"ns3:Test_Service": {"@xmlns:ns3": "http://www.CCKS.org/XRT/Form","ns3:fname": "mark","ns3:lname": "joye","ns3:CarCompany": "saab","ns3:CarNumber": "9741","ns3:IsInsured": "true","ns3:safty": [ "ABS", "AirBags", "childdoorlock" ],"ns3:CarDescription": "test Car","collections": [{"XYZ": "1","PQR": "11","contactdetails": [{"contname": "DOM","contnumber": "8787"},{"contname": "COM","contnumber": "4564","addtionaldetails": [ { "description": "54657667" } ]},{"contname": "gf","contnumber": "123","addtionaldetails": [ { "description": "123" } ]}]}]}}