gpt4 book ai didi

c# - c#解析Json数据转xml

转载 作者:太空宇宙 更新时间:2023-11-03 23:44:49 26 4
gpt4 key购买 nike

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

}
}

关于c# - c#解析Json数据转xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27874989/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com