【问题标题】:Parsing Json data in c# to convert to xml在c#中解析Json数据以转换为xml
【发布时间】:2015-01-10 10:07:17
【问题描述】:

在我的 asp.net 应用程序中,我必须将 xml 格式的数据传递给存储过程。

我所拥有的 Json 格式的数据如下:

{
  "total": 10,
  "page": 1,
  "records": 100,
  "rows": [
    {
      "id": 0,
      "cell": [
        "217121",
        "1001",
        "CAMRY",
        "2532",
        "2532",
        "0",
        "36",
        "8",
        "13",
        "0.03",
        "0.15",
        "0.15",
        "0.10",
        "0.14"
      ]
    },
    {
      "id": 1,
      "cell": [
        "217121",
        "1001",
        "CAMRY",
        "2540",
        "2540",
        "0",
        "6",
        "18",
        "13",
        "0.29",
        "0.14",
        "0.14",
        "0.21",
        "0.27"
      ]
    },
    {
      "id": 2,
      "cell": [
        "217121",
        "1001",
        "CAMRY",
        "2546",
        "2546",
        "0",
        "9",
        "3",
        "5",
        "0.13",
        "0.35",
        "0.35",
        "0.24",
        "-0.32"
      ]
    },
    {
      "id": 3,
      "cell": [
        "217121",
        "1001",
        "CAMRY",
        "2548",
        "2548",
        "0",
        "29",
        "8",
        "14",
        "0.30",
        "0.16",
        "0.16",
        "0.23",
        "-0.41"
      ]
    },
    {
      "id": 4,
      "cell": [
        "217121",
        "1001",
        "CAMRY",
        "2550",
        "2550",
        "0",
        "31",
        "17",
        "7",
        "0.12",
        "0.10",
        "0.10",
        "0.11",
        "-0.14"
      ]
    },
    {
      "id": 5,
      "cell": [
        "217121",
        "1001",
        "CAMRY",
        "2554",
        "2554",
        "0",
        "20",
        "37",
        "3",
        "0.13",
        "0.10",
        "0.10",
        "0.11",
        "-0.62"
      ]
    }
  ]
}

在我的按钮单击中,我想解析它并将其转换为 xml。我正在使用命名空间

using Newtonsoft.Json.Linq;

并使用以下代码,

protected void TEST_Click(object sender, EventArgs e)
{
    var modelJson = hdnModelObject.Value;
    var obj=JObject.Parse(modelJson);
}

但不知道如何循环它。卡在这条线上。如果有任何建议会很棒。 问候

【问题讨论】:

    标签: c# xml json linq


    【解决方案1】:

    如何阅读documentation on Json.Net

    DeserializeXmlNode

    JsonConvert 的第二个辅助方法是 DeserializeXmlNode()。这 方法接受 JSON 文本并将其反序列化为 XmlNode。

    因为有效的 XML 必须有一个 JSON 传递给的根元素 DeserializeXmlNode 在根 JSON 对象中应该有一个属性。 如果根 JSON 对象具有多个属性,则重载 还需要使用元素名称。一个根元素 name 将被插入到反序列化的 XmlNode 中。

    你的json有问题,你需要添加一个根元素,所以你可以这样做.NETFiddle

    这是您当前的字符串格式的 json:

    var json =
                @"{""total"":10,""page"":1,""records"":100,""rows"":[{""id"":0,""cell"":[""217121"",""1001"",""CAMRY"",""2532"",""2532"",""0"",""36"",""8"",""13"",""0.03"",""0.15"",""0.15"",""0.10"",""0.14""]},{""id"":1,""cell"":[""217121"",""1001"",""CAMRY"",""2540"",""2540"",""0"",""6"",""18"",""13"",""0.29"",""0.14"",""0.14"",""0.21"",""0.27""]},{""id"":2,""cell"":[""217121"",""1001"",""CAMRY"",""2546"",""2546"",""0"",""9"",""3"",""5"",""0.13"",""0.35"",""0.35"",""0.24"",""-0.32""]},{""id"":3,""cell"":[""217121"",""1001"",""CAMRY"",""2548"",""2548"",""0"",""29"",""8"",""14"",""0.30"",""0.16"",""0.16"",""0.23"",""-0.41""]},{""id"":4,""cell"":[""217121"",""1001"",""CAMRY"",""2550"",""2550"",""0"",""31"",""17"",""7"",""0.12"",""0.10"",""0.10"",""0.11"",""-0.14""]},{""id"":5,""cell"":[""217121"",""1001"",""CAMRY"",""2554"",""2554"",""0"",""20"",""37"",""3"",""0.13"",""0.10"",""0.10"",""0.11"",""-0.62""]}]}";
    

    这是包裹在根元素中的 json 字符串:

    var jsonWithRoot = string.Format("{{'root': {0}}}",json);
    

    这是你的 XMLDocument:

    XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonWithRoot);
    

    控制台应用示例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml;
    using Newtonsoft.Json;
    using System.IO;
    
    
    public class Program
    {
    
        public static void Main()
        {
            var json =
                @"{""total"":10,""page"":1,""records"":100,""rows"":[{""id"":0,""cell"":[""217121"",""1001"",""CAMRY"",""2532"",""2532"",""0"",""36"",""8"",""13"",""0.03"",""0.15"",""0.15"",""0.10"",""0.14""]},{""id"":1,""cell"":[""217121"",""1001"",""CAMRY"",""2540"",""2540"",""0"",""6"",""18"",""13"",""0.29"",""0.14"",""0.14"",""0.21"",""0.27""]},{""id"":2,""cell"":[""217121"",""1001"",""CAMRY"",""2546"",""2546"",""0"",""9"",""3"",""5"",""0.13"",""0.35"",""0.35"",""0.24"",""-0.32""]},{""id"":3,""cell"":[""217121"",""1001"",""CAMRY"",""2548"",""2548"",""0"",""29"",""8"",""14"",""0.30"",""0.16"",""0.16"",""0.23"",""-0.41""]},{""id"":4,""cell"":[""217121"",""1001"",""CAMRY"",""2550"",""2550"",""0"",""31"",""17"",""7"",""0.12"",""0.10"",""0.10"",""0.11"",""-0.14""]},{""id"":5,""cell"":[""217121"",""1001"",""CAMRY"",""2554"",""2554"",""0"",""20"",""37"",""3"",""0.13"",""0.10"",""0.10"",""0.11"",""-0.62""]}]}";
    
    
              var jsonWithRoot = string.Format("{{'root': {0}}}",json);
              XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonWithRoot);
    
              using (var stringWriter = new StringWriter())
               using (var xmlTextWriter = XmlWriter.Create(stringWriter))
               {
                 doc.WriteTo(xmlTextWriter);
                 xmlTextWriter.Flush();
                 Console.Write(stringWriter.GetStringBuilder().ToString());
               }
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 JArray 来解析您的 json 字符串,并对数组进行迭代。

      JArray a = JArray.Parse(json);
      foreach(var obj in a) {
          //iterator the array
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 2012-10-15
        • 1970-01-01
        • 1970-01-01
        • 2015-06-30
        相关资源
        最近更新 更多