我目前正在使用Newtonsoft Json.NET库来解析Json编码数据,然后将节点转换为所需的数据类型。
以下是一个已经为我工作的示例数据结构:
{
"ToolUtility": {
"SharedBrushListModel": {
"SelectedBrushInstanceID": 786
},
"SomethingElse": 42
}
}
以下是我如何与这些数据进行互动:
// Parse data and collate into groups of properties.
var store = new Dictionary<string, Dictionary<string, JProperty>>();
foreach (var group in JObject.Parse(jsonInput)) {
var storeGroup = new Dictionary<string, JProperty>();
store[group.Key] = storeGroup;
foreach (var property in group.Value.Children<JProperty>())
storeGroup[property.Name] = property;
}
// Convert nodes into the desired types.
var utilityNode = store["ToolUtility"];
var model = utilityNode["SharedBrushListModel"].ToObject<SharedBrushListModel>();
int somethingElse = utilityNode["SomethingElse"].ToObject<int>();
// Convert desired types back to nodes.
var nodeA = JToken.FromObject(model);
var nodeB = JToken.FromObject(somethingElse);
// Encode back to Json.
var outputRoot = new JObject();
outputRoot["ToolUtility"] = new JObject();
outputRoot["ToolUtility"]["SharedBrushListModel"] = nodeA;
outputRoot["ToolUtility"]["SomethingElse"] = nodeB;
string json = outputRoot.ToString(formatter);
这个API的优点在于我可以解析Json编码数据而不包含任何类型注释,然后在稍后解析时将节点显式转换为专用类型。
普通的.NET有没有办法用XML做同样的事情?
<?xml version="1.0" encoding="utf-8"?>
<root>
<ToolUtility>
<SharedBrushListModel>
<SelectedBrushInstanceID>786</SelectedBrushInstanceID>
</SharedBrushListModel>
<SomethingElse>42</SomethingElse>
</ToolUtility>
</root>
我想使用标准的.NET库来实现这一点,而不是需要任何第三方库,如Json.NET。
包含额外库的问题在于目标环境(Unity游戏引擎)中出现了许多问题,其中多个资产包含相同的库。
答案 0 :(得分:3)
查看System.Xml.Linq
命名空间。这是概念的映射:
JToken
=&gt; XNode
JToken.Parse(string json)
=&gt; XDocument.Parse(string xml)
JToken.ToObject<T>()
=&gt; XNode.ToObject<T>()
(扩展方法如下所示) ToObject<T>
方法默认情况下实际上不在XNode
。这是扩展方法:
public T ToObject<T>(this XNode node)
{
var serializer = new XmlSerializer(typeof(T));
using (var reader = node.CreateReader())
{
return (T)serializer.Deserialize(reader);
}
}
答案 1 :(得分:2)
C#具有称为LINQ To XML的内置功能。以下是查询语法功能强大的示例:
IEnumerable<XElement> partNos =
from item in purchaseOrder.Descendants("Item")
where (int) item.Element("Quantity") *
(decimal) item.Element("USPrice") > 100
orderby (string)item.Element("PartNumber")
select item;
您可以使用如下的语法创建XML:
XElement contacts =
new XElement("Contacts",
new XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144",
new XAttribute("Type", "Home")),
new XElement("phone", "425-555-0145",
new XAttribute("Type", "Work")),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
)
);
答案 2 :(得分:0)
如果使用类并在其中定义数据,则可以将数据(该类的对象)序列化和反序列化为XML:
var serializer = new XmlSerializer(SomeList.GetType());
using (var writer = XmlWriter.Create(SomeFile))
serializer.Serialize(writer, SomeList);
和
var serializer = new XmlSerializer(typeof(List<SomeClass>));
using (XmlReader reader = XmlReader.Create(SomeFile))
SomeList = (List<SomeClass>)serializer.Deserialize(reader);