如何使用NewtonSoft Json.Net将Json字典反序列化为平面类(How to deserialize a Json dictionary to a flat class with New

编程入门 行业动态 更新时间:2024-10-28 09:17:53
如何使用NewtonSoft Json.Net将Json字典反序列化为平面类(How to deserialize a Json dictionary to a flat class with NewtonSoft Json.Net)

我从一个我无法控制的服务得到类似的Json:

"SomeKey": { "Name": "Some name", "Type": "Some type" }, "SomeOtherKey": { "Name": "Some other name", "Type": "Some type" }

我试图通过使用NewtonSoft Json.Net将该字符串反序列化为.Net类,因为我的类现在看起来像这样:

public class MyRootClass { public Dictionary<String, MyChildClass> Devices { get; set; } } public class MyChildClass { [JsonProperty("Name")] public String Name { get; set; } [JsonProperty("Type")] public String Type { get; set; } }

然而,我会更喜欢我的类的扁平版本,没有这样的字典:

public class MyRootClass { [JsonProperty("InsertMiracleCodeHere")] public String Key { get; set; } [JsonProperty("Name")] public String Name { get; set; } [JsonProperty("Type")] public String Type { get; set; } }

我不知道如何实现这一点,因为我不知道如何访问customconverter中的键,如下所示:

http://blog.maskalik.com/asp-net/json-net-implement-custom-serialization

为了防止有人关心,可以找到一个页面的链接,其中可以找到我获得的Json字符串的实际样本: Ninjablocks Rest API文档与json示例

i am getting similar Json from a service that i do not control:

"SomeKey": { "Name": "Some name", "Type": "Some type" }, "SomeOtherKey": { "Name": "Some other name", "Type": "Some type" }

I am trying to deserialize that string to a .Net-class by using NewtonSoft Json.Net which works pretty well as my classes right now look like this:

public class MyRootClass { public Dictionary<String, MyChildClass> Devices { get; set; } } public class MyChildClass { [JsonProperty("Name")] public String Name { get; set; } [JsonProperty("Type")] public String Type { get; set; } }

I would however much more prefer a flattened version of my class, without a dictionary like this:

public class MyRootClass { [JsonProperty("InsertMiracleCodeHere")] public String Key { get; set; } [JsonProperty("Name")] public String Name { get; set; } [JsonProperty("Type")] public String Type { get; set; } }

I have however no clue of how to achieve this because i don't know how to access the keys in a customconverter like this:

http://blog.maskalik.com/asp-net/json-net-implement-custom-serialization

Just in case someone cares, a link to a page where actual samples of the Json strings i get can be found: Ninjablocks Rest API documentation with json samples

最满意答案

我不知道是否有办法用JSON.NET做到这一点。 也许你是在思考它。 如何为反序列化JSON创建单独的DTO类型,然后将结果投影到适合您的域的其他类型。 例如:

public class MyRootDTO { public Dictionary<String, MyChildDTO> Devices { get; set; } } public class MyChildDTO { [JsonProperty("Name")] public String Name { get; set; } [JsonProperty("Type")] public String Type { get; set; } } public class MyRoot { public String Key { get; set; } public String Name { get; set; } public String Type { get; set; } }

然后你可以按如下方式映射它:

public IEnumerable<MyRoot> MapMyRootDTO(MyRootDTO root) { return root.Devices.Select(r => new MyRoot { Key = r.Key, Name = r.Value.Name Type = r.Value.Type }); }

I don't know if there's a way to do that with JSON.NET. Maybe you're overthinking it. How about creating a separate DTO type for deserializing the JSON, then projecting the result into another type suited more to your domain. For example:

public class MyRootDTO { public Dictionary<String, MyChildDTO> Devices { get; set; } } public class MyChildDTO { [JsonProperty("Name")] public String Name { get; set; } [JsonProperty("Type")] public String Type { get; set; } } public class MyRoot { public String Key { get; set; } public String Name { get; set; } public String Type { get; set; } }

Then you can map it as follows:

public IEnumerable<MyRoot> MapMyRootDTO(MyRootDTO root) { return root.Devices.Select(r => new MyRoot { Key = r.Key, Name = r.Value.Name Type = r.Value.Type }); }

更多推荐

本文发布于:2023-07-26 05:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   序列   字典   平面   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!