{
"Profile": {
"dProperty1": {
"a": "value",
"b": "value",
"c": "value",
"d": "value",
"e": "value"
},
"dProperty2": {
"a": "value",
"b": "value",
"d": "value",
"e": "value"
},
"dProperty3": {
"a": "value",
"b": "value",
"d": "value",
"e": "value"
}
}
}
我有一个 JSON 对象,它可以有任意数量的动态属性。所有属性都是主要由相同字段组成的对象。我如何在 C# 中将此 JSON 解析为强类型对象?
最佳答案
如果你必须有强类型的结果,我会将 Profile 反序列化为属性叠加的字典
class AbscdeClass
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
}
class JsonBody
{
public Dictionary<string, AbscdeClass> Profile { get; set; }
}
并将原始 JSON 文本解析为
JsonBody json = JsonConvert.DeserializeObject<JsonBody>(jsonString);
关于c# - 将具有可变属性的 JSON 对象解析为强类型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202496/