zoukankan      html  css  js  c++  java
  • json.net xml转换为json格式时,如何将指定节点转换成数组

     using System.Xml.Linq;
     using Newtonsoft.Json;
     
     Response.ContentType = "application/json";
     XDocument xdoc = XDocument.Load(path);
     Response.Write(JsonConvert.SerializeXNode(xdoc));

    xml 片段 :

    <specialty nameCN="电测">
            <step>
                <signer staffID="800706" nameCN=""><![CDATA[]]></signer>
            </step>
            <step>
                <signer staffID="090477" nameCN=""><![CDATA[]]></signer>
            </step>
        </specialty>
        <specialty nameCN="节能">
            <step>
                <signer staffID="800608" nameCN=""><![CDATA[]]></signer>
                <signer staffID="800808" nameCN=""><![CDATA[]]></signer>
            </step>
            <step>
                <signer staffID="800602" nameCN=""><![CDATA[]]></signer>
                <signer staffID="800803" nameCN=""><![CDATA[]]></signer>
            </step>
        </specialty>

     输出 json 结果:

    {
      "@nameCN": "电测",
      "step": [
        {
          "signer": {
            "@staffID": "800706",
            "@nameCN": "",
            "#cdata-section": 
          }
        },
        {
          "signer": {
            "@staffID": "090477",
            "@nameCN": "",
            "#cdata-section": 
          }
        }
      ]
    },
    {
      "@nameCN": "节能",
      "step": [
        {
          "signer": [
            {
              "@staffID": "800608",
              "@nameCN": "",
              "#cdata-section": 
            },
            {
              "@staffID": "800808",
              "@nameCN": "",
              "#cdata-section": 
            }
          ]
        },
        {
          "signer": [
            {
              "@staffID": "800602",
              "@nameCN": "",
              "#cdata-section": 
            },
            {
              "@staffID": "800803",
              "@nameCN": "",
              "#cdata-section": 
            }
          ]
        }
      ]
    }

    上面的结果 用红色标记出来的就是差别,step下有多个signer节点时,输出结果signer是数组

    只有1个signer节点 输出signer不是数组,如何在只有一个signer节点时也输出为数组

  • 相关阅读:
    TCP协议简单套接字通信 客户端
    TCP协议简单套接字通信 服务端
    java课程作业--动手动脑
    Java方法课程作业1,2,3
    java猜数字(实验任务五)
    课程作业02(关于Java的几点讨论)
    java多个int型数据累加求和
    java基本登录界面
    大道至简第一章观后感——java伪代码
    《大道至简—软件编程者的思想》观后感
  • 原文地址:https://www.cnblogs.com/dekevin/p/4128729.html
Copyright © 2011-2022 走看看