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

时间:2015-01-10 10:07:18

标签: c# xml json linq

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

但不确定如何循环它。卡在这条线上。如果有任何建议会很棒。 此致

2 个答案:

答案 0 :(得分:3)

如何阅读documentation on Json.Net

<强> DeserializeXmlNode

  

JsonConvert上的第二个辅助方法是DeserializeXmlNode()。这个   method接受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());
           }

    }
}

答案 1 :(得分:0)

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

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