草庐IT

c# - 在启用强制数组的情况下将 JSON 转换为 XML 时出现问题

coder 2024-07-01 原文

在我的工作中,我需要将 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 时,它会在 XML.Resulting 中添加一个属性,如 xmlns:json=' http://james.newtonking.com/projects/json ' & 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=' http://james.newtonking.com/projects/json ' 这样的属性 & json:Array='true' 在转换后的 XML 中。下面添加转换后的 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>

最佳答案

您正在尝试合并两个错误的 XML namespace ,如果您从 collections 标签及其子标签中删除 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" } ]}]}]}}

关于c# - 在启用强制数组的情况下将 JSON 转换为 XML 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735153/

有关c# - 在启用强制数组的情况下将 JSON 转换为 XML 时出现问题的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  4. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  5. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  6. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  7. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  8. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  9. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  10. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

随机推荐