在 c# 中解析 Json 数据以转换为 xml
本文关键字:转换 xml 数据 Json | 更新日期: 2023-09-27 18:37:04
在我的 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);
}
但不知道如何循环它。 卡在这一行。如果有任何疑问,那就太好了。问候
如何从文档中阅读有关 Json.Net
反序列化XmlNode
JsonConvert 上的第二个帮助程序方法是 DeserializeXmlNode()。这 方法采用 JSON 文本并将其反序列化为 XmlNode。
因为有效的 XML 必须有一个 JSON 传递给的根元素 反序列化XmlNode在根JSON对象中应该有一个属性。 如果根 JSON 对象具有多个属性,则重载 还取一个元素名称应该使用。具有该根元素的根元素 名称将入到反序列化的 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
}