在 c# 中解析 Json 数据以转换为 xml

Parsing Json data in c# to convert to xml

在我的 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);
}

但不确定如何循环播放。卡在这条线上。如果有任何建议,那就太好了。 问候

如何阅读 documentation on Json.Net

DeserializeXmlNode

The second helper method on JsonConvert is DeserializeXmlNode(). This method takes JSON text and deserializes it into a XmlNode.

Because valid XML must have one root element the JSON passed to DeserializeXmlNode should have one property in the root JSON object. If the root JSON object has multiple properties then the overload that also takes an element name should be used. A root element with that name will be inserted into the deserialized 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());
           }

    }
}

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

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