.net 通过处理名称空间和数组将XML转换为JSON

lxkprmvk  于 2023-01-03  发布在  .NET
关注(0)|答案(1)|浏览(90)

我想把XML转换成JSON。但是由于命名空间、前缀和JSON数组的问题,我面临的问题很少。

输入XML

<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
  <OrganizationId>123</OrganizationId>
  <ActionId>123</ActionId>
  <SessionId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
  <EnterpriseUrl>qwe</EnterpriseUrl>
  <PartnerUrl>qwe</PartnerUrl>
  <Notification>
    <Id>123</Id>
    <sObject xsi:type="sf:Opportunity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
      <sf:Id>ao123</sf:Id>
      <sf:Amount>60000.0</sf:Amount>
      <sf:CreatedDate>2014-11-26T14:45:52.000Z</sf:CreatedDate>
      <sf:IsClosed>false</sf:IsClosed>
    </sObject>
  </Notification>
</notifications>

输出JSON

{
  "notifications": {
    "OrganizationId": "123",
    "ActionId": "123",
    "SessionId": {
      "@nil": "true"
    },
    "EnterpriseUrl": "qwe",
    "PartnerUrl": "qwe",
    "Notification": [
      {
        "Id": "ao123",
        "sObject": {
          "@type": "sf:Opportunity",
          "Id": "ao123",
          "Amount": "60000.0",
          "CreatedDate": "2014-11-26T14:45:52.000Z",
          "IsClosed": "false"
        }
      }
    ]
  }
}

以下是我面临的几个问题

  • XML的命名空间和前缀不应出现在json中。
  • Notification应该是一个json数组,即使我收到一个项目

到目前为止,我所尝试的是使用method删除名称空间和前缀,然后使用JsonConvert.SerializeXNode将其转换为JSON。
我觉得这些步骤更多的是数据操作,我正在寻找一些好的方法来实现同样的目的。所以我尝试使用XSLT,我能够删除名称空间前缀.fiddle link for XSLT。但我不确定如何使用XSLT删除前缀,然后将XML转换为预期的JSON格式(可能使用XSLT xml-to-json选项)。正在寻找使用XSLT的解决方案

bzzcjhmw

bzzcjhmw1#

你可以试试这个代码

var xDoc = XDocument.Parse(xmlString);

xDoc.Root.DescendantNodesAndSelf().OfType<XElement>().Attributes().Where(att => att.IsNamespaceDeclaration).Remove();

xDoc.Root.DescendantNodesAndSelf().OfType<XElement>().ToList().ForEach(node => node.Name = node.Name.LocalName);

var json = JsonConvert.SerializeXNode(xDoc, Newtonsoft.Json.Formatting.Indented, false);

或者假设https://xsltfiddle.liberty-development.net/aUPRNo/1在XSLT之后,您的xml是

<?xml version="1.0" encoding="UTF-8"?>
<notifications>
   <OrganizationId>123</OrganizationId>
   <ActionId>123</ActionId>
   <SessionId nil="true" />
   <EnterpriseUrl>qwe</EnterpriseUrl>
   <PartnerUrl>qwe</PartnerUrl>
   <Notification>
      <Id>123</Id>
      <sObject type="sf:Opportunity">
         <Id>ao123</Id>
         <Amount>60000.0</Amount>
         <CreatedDate>2014-11-26T14:45:52.000Z</CreatedDate>
         <IsClosed>false</IsClosed>
      </sObject>
   </Notification>
</notifications>
<notifications>
   <OrganizationId>123</OrganizationId>
   <ActionId>123</ActionId>
   <SessionId nil="true" />
   <EnterpriseUrl>qwe</EnterpriseUrl>
   <PartnerUrl>qwe</PartnerUrl>
   <Notification>
      <Id>123</Id>
      <sObject type="sf:Opportunity">
         <Id>ao123</Id>
         <Amount>60000.0</Amount>
         <CreatedDate>2014-11-26T14:45:52.000Z</CreatedDate>
         <IsClosed>false</IsClosed>
      </sObject>
   </Notification>
</notifications>

您可以使用此代码将其转换为json

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
    
XmlNode node = xmlDoc.SelectSingleNode("notifications");

var json =  JsonConvert.SerializeXmlNode(node, Newtonsoft.Json.Formatting.Indented, false);

在这两种情况下,输出是相同的

{
 "notifications": {
  "OrganizationId": "123",
  "ActionId": "123",
  "SessionId": {
    "@nil": "true"
  },
  "EnterpriseUrl": "qwe",
  "PartnerUrl": "qwe",
  "Notification": {
    "Id": "123",
    "sObject": {
      "@type": "sf:Opportunity",
      "Id": "ao123",
      "Amount": "60000.0",
      "CreatedDate": "2014-11-26T14:45:52.000Z",
      "IsClosed": "false"
    }
  }
}
}

相关问题