将具有可变属性的JSON对象解析为强类型对象 [英] Parsing JSON Object with variable properties into strongly typed object
本文介绍了将具有可变属性的JSON对象解析为强类型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
"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解析为强类型对象?
I have a JSON object, which can have any number of dynamic properties. All the properties are objects that consist of mostly of the same fields. How can i parse this JSON into a strongly typed object in C#?
推荐答案
如果必须使用强类型化的结果,我会反序列化Profile作为属性叠加的字典
If you must have strongly typed result I would deserialize Profile as a dictionary of superposition of properties
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文本解析为
and parse original JSON text as
JsonBody json = JsonConvert.DeserializeObject<JsonBody>(jsonString);
这篇关于将具有可变属性的JSON对象解析为强类型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文