我从一个我无法控制的服务得到类似的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 }); }更多推荐
发布评论