我从一个我无法控制的服务中获取到了类似的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; }
}
然而,我不知道如何做到这一点,因为我不知道如何在这样的自定义转换器中访问键:
http://blog.maskalik.com/asp-net/json-net-implement-custom-serialization
如果有人在意,这里是一个链接,可以找到我获取的Json字符串的实际样例:Ninjablocks Rest API documentation with json samples