【发布时间】:2018-11-13 10:54:55
【问题描述】:
我正在使用第三方 API,因此无法更改响应结构。 作为回应,我得到了这样的结果:
{
"Code": "SomeCode",
"Name": "Some Name",
"IsActive": true,
"Prop13": {
"LongId": "12",
"ShortId": "45"
},
"Prop26": {
"LongId": "12",
"ShortId": "45"
},
"Prop756": {
"LongId": "12",
"ShortId": "45"
}
}
我需要将其解析为下一个类:
public class Class1
{
public string Code { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public Dictionary<string, PropertiesClass> Properties { get; set; }
}
public class PropertiesClass
{
public int LongId { get; set; }
public int ShortId { get; set; }
}
属性“Prop13”、“Prop26”等是动态响应的,它们都不可能存在,或者完全是其他名称。因此,我必须只将 Code、Name 和 IsActive(始终存在)存储到类属性中,所有其他都应该存储到 Dictionary 中。并且属性的名称应该作为键存储在字典中。
我在 https://www.newtonsoft.com/json/help/html/Introduction.htm 中找不到任何可以帮助我的东西
【问题讨论】:
-
不是动态对象
-
在这里查看示例newtonsoft.com/json/help/html/DeserializeExtensionData.htm 这样您就可以使所有内容自包含。