C# Json.Net映射到字典或其他
考虑到这一点:C# Json.Net映射到字典或其他,c#,json,serialization,dictionary,json.net,C#,Json,Serialization,Dictionary,Json.net,考虑到这一点: { "id": "1", "fields": [ "a", "b", "c" ], "otherfields": [ "n" ], "collection": { "element1": [ { "7": { "sub_id": "7",
{
"id": "1",
"fields": [
"a",
"b",
"c"
],
"otherfields": [
"n"
],
"collection": {
"element1": [
{
"7": {
"sub_id": "7",
"sub_name": "SN7"
},
"9": {
"sub_id": "9",
"sub_name": "SN9"
}
}
]
}
}
并给出了以下类别:
public class Element
{
[JsonProperty("sub_id")]
public string SubId { get; set; }
[JsonProperty("sub_name")]
public string SubName { get; set; }
}
public class Element1
{
[JsonProperty(?)] // <"7", Element> and <"9", Element> ???
public IDictionary<String, Element> Elements { get; set; }
}
public class Collection
{
[JsonProperty("element1")]
public IList<Element1> Element1 { get; set; }
}
public class Example
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("fields")]
public IList<string> Fields { get; set; }
[JsonProperty("otherfields")]
public IList<string> Otherfields { get; set; }
[JsonProperty("collection")]
public Collection Collection { get; set; }
}
公共类元素
{
[JsonProperty(“子实体id”)]
公共字符串子ID{get;set;}
[JsonProperty(“子公司名称”)]
公共字符串子名称{get;set;}
}
公共类元素1
{
[JsonProperty(?)//和???
公共IDictionary元素{get;set;}
}
公共类集合
{
[JsonProperty(“元素1”)]
公共IList元素1{get;set;}
}
公开课范例
{
[JsonProperty(“id”)]
公共字符串Id{get;set;}
[JsonProperty(“字段”)]
公共IList字段{get;set;}
[JsonProperty(“其他字段”)]
公共IList其他字段{get;set;}
[JsonProperty(“集合”)]
公共集合{get;set;}
}
是否可能(以及如何)将json对象反序列化到给定的类?或者,将json对象映射到另一个结构类的最佳方法是什么
提前感谢。您不需要Element1-json中的Element1属性是一个带有字典的数组
public class Collection
{
[JsonProperty("element1")]
public IList<IDictionary<String, Element>> Element1 { get; set; }
}
公共类集合
{
[JsonProperty(“元素1”)]
公共IList元素1{get;set;}
}