简体   繁体   English

如何执行与JSON之间的数据表的序列化和反序列化

[英]How to perform serialization and deserialization of datatables to and from JSON

I am trying to find a way to both serialize and deserialize a dataset with one datatable to JSON and from JSON back to a dataset / datatable using JSON.NET, however all the samples and examples I have found are only serializing from a dataset or datatable to JSON and never two way. 我正在尝试找到一种方法来序列化和反序列化具有一个数据表的数据集到JSON,并使用JSON.NET从JSON返回到数据集/数据表,但是我发现的所有示例和示例仅从数据集或数据表进行序列化JSON,永远不会两种方式。 We have a system that deals with XML serialized datasets and datatables that we need to still retain in that format but allow certain UI views to render the data as JSON. 我们有一个处理XML序列化数据集和数据表的系统,我们仍然需要保留该格式,但是允许某些UI视图将数据呈现为JSON。

Data can have null values and that's valid. 数据可以具有空值,并且是有效的。

Any help would be much appreciated. 任何帮助将非常感激。

Example (One way serialization): 示例(单向序列化):
http://www.west-wind.com/weblog/posts/2008/Sep/03/DataTable-JSON-Serialization-in-JSONNET-and-JavaScriptSerializer http://www.west-wind.com/weblog/posts/2008/Sep/03/DataTable-JSON-Serialization-in-JSONNET-and-JavaScriptSerializer

This all depends how you want to manage the deserialization. 这一切都取决于您要如何管理反序列化。 Personally, I like to go with the LINQ-based approach which works in the following way: 就个人而言,我喜欢采用基于LINQ的方法,该方法可通过以下方式工作:

// Get the children of the JSON result (This example is from my own code,
// in which case I have one big "result" node which contains a bunch of
// children that I am interested in deserializing.
var jsonChildren = JObject.Parse(response)["results"].Children();

// Now use a LINQ statement to deserialize. For example...
var jsonResults = jsonChildren.Select(x =>
{
    new MyObject
    {
        Prop1 = x["Var1"],
        Prop2 = x["Var2"],
    }
});

What it all boils down to is that you use the JSON children IEnumerable like you would an array of key value pairs, accessing the necessary descendants using square-bracket syntax. 归结为,您就像使用键值对数组一样使用JSON子级IEnumerable,使用方括号语法访问必要的后代。 The LINQ just makes things a bit cleaner. LINQ只是使事情变得更干净。

Update 更新

Not sure if this applies to your case, but here is an interesting article on the subject that uses dynamic objects http://www.west-wind.com/weblog/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing 不知道这是否适用于您的情况,但这是有关使用动态对象的主题的有趣文章http://www.west-wind.com/weblog/posts/2012/Aug/30/Using-JSONNET-for-动态JSON-解析

The following link is to the source for Newtonsoft.Json.Converters.DataTableConverter.cs which does what you want, it's pretty straight forward and seems like the best route. 以下链接是Newtonsoft.Json.Converters.DataTableConverter.cs的源代码,它可以完成您想要的操作,这很简单,而且似乎是最佳途径。

https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/DataTableConverter.cs https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/DataTableConverter.cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM