gpt4 book ai didi

c# - 复杂对象的JSON解析

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:24 25 4
gpt4 key购买 nike

下面给出的是JSON响应的类型,

{
"?xml":{
"@version":"1.0",
"@encoding":"iso-8859-1"
},
"xmlreport":{
"@title":"ABC: TEST Most Saved2",
"@dates":"Week of May 19,2013",
"columns":{
"column":[
{
"@name":"Page",
"@type":"dimension",
"#text":"Page"
},
{
"@name":"Events",
"@type":"metric",
"@hastotals":"true",
"#text":"Events"
}
]
},
"rows":{
"row":[
{
"@rownum":"1",
"cell":[
{
"@columnname":"page",
"@csv":"\"http://www.ABC.com/profile/recipebox\"",
"#text":"http://www.ABC.com/profile/recipebox"
},
{
"@columnname":"events",
"@percentage":"\"0.1%\"",
"#text":"489"
}
]
},
{
"@rownum":"2",
"cell":[
{
"@columnname":"page",
"@csv":"\"http://www.ABC.com/recipes/peanut-butter-truffle-brownies/c5c602e4-007b-43e0-aaab-2f9aed89524c\"",
"#text":"http://www.ABC.com/recip...c602e4-007b-43e0-aaab-2f9aed89524c"
},
{
"@columnname":"events",
"@percentage":"\"0.0%\"",
"#text":"380"
}
]
}
]
},
"totals":{
"pagetotals":{
"total":{
"@columnname":"events",
"@value":"1820.000000",
"#text":"1,820 (0.2%)"
}
},
"reporttotals":{
"total":{
"@columnname":"events",
"@value":"7838.000000",
"#text":"7,838 (0.8%)"
}
},
"timeperiodtotals":{
"total":{
"@columnname":"events",
"@value":"955774.000000",
"#text":"955,774 (100.0%)"
}
}
}
}
}

我无法解析该对象。能否请您帮助我解决解析后如何读取属性和元素的问题。我正在使用 C#

{ 
XmlDocument doc = new XmlDocument();
doc.LoadXml(XML);

string jsonText = JsonConvert.SerializeXmlNode(doc);
//var result = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(jsonText, "xmlreport");
var results = JsonConvert.DeserializeObject<dynamic>(jsonText);

JToken token = JObject.Parse(jsonText);
var report = token["xmlreport"];
}

最佳答案

我对这个问题的理解是你有一些 Xml,你需要发送 json。在我们开始编写代码之前有几点:

1) 不要直接将 xml 转换为 json,因为它会导致问题

2) 将xml解析成你端的对象,然后计算出返回的格式;将传入和传出的内容解耦将允许其中一个接口(interface)在未来发生变化而不会影响另一个接口(interface),因为您可以调整映射

所以,进入代码...

本质上是将 xml 解析为对象以允许进一步处理,然后作为 json 推送。

class Program
{
private static string starting =
"<xmlreport title=\"ABC: TEST Most Saved2\" dates=\"Week of May 19,2013\"><columns><column name=\"Page\" type=\"dimension\">Page</column><column name=\"Events\" type=\"metric\" hastotals=\"true\">Events</column></columns><rows><row rownum=\"1\"><cell columnname=\"page\" csv=\"http://www.ABC.com/profile/recipebox\">http://www.ABC.com/profile/recipebox</cell><cell columnname=\"events\" percentage=\"0.1%\">489</cell></row><row rownum=\"2\"><cell columnname=\"page\" csv=\"http://www.ABC.com/recipes/peanut-butter-truffle-brownies/c5c602e4-007b-43e0-aaab-2f9aed89524c\">http://www.ABC.com/recipes/peanut-butter-truffle-brownies/c5c602e4-007b-43e0-aaab-2f9aed89524c</cell><cell columnname=\"events\" percentage=\"0.0%\">380</cell></row></rows><totals><pagetotals><total columnname=\"events\" value=\"1820.00000\">1,820 (0.2%)</total></pagetotals><reporttotals><total columnname=\"events\" value=\"7838.000000\">7,838 (0.8%)</total></reporttotals><timeperiodtotals><total columnname=\"events\" value=\"955774.000000\">955,774 (100.0%)</total></timeperiodtotals></totals></xmlreport>";


static void Main(string[] args)
{
// parse from xml to objects
StringReader reader = new StringReader(starting);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(XmlReport));
var xmlreport = (XmlReport)xmlSerializer.Deserialize(reader);

// todo: do some process mapping ...

// parse out as json
var json = JsonConvert.SerializeObject(xmlreport);

Console.WriteLine(json);
Console.ReadLine();
}
}

[Serializable]
[XmlRoot(ElementName = "xmlreport")]
public class XmlReport
{
[XmlAttribute(AttributeName = "title")]
public string Title { get; set; }
[XmlAttribute(AttributeName = "dates")]
public string Dates { get; set; }

[XmlArray(ElementName = "columns")]
[XmlArrayItem(typeof(Column), ElementName = "column")]
public Collection<Column> Columns { get; set; }
}

[Serializable]
public class Column
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}

如果我没有正确解释它,我已经尝试将 json 解析为原始 xml 以开始如此应用程序。我还没有完成整个结构,但我希望上面的示例能让您了解如何完成其​​余部分。

希望这对您有所帮助。

关于c# - 复杂对象的JSON解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16850642/

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