我想使用JSON.Net反序列化此架构。
{
"color" : {
"type" : "String",
"description" : "What color do you want your taco",
"required" : false,
"default" : "Green",
"options" : [ "Green", "Blue", "Red"]
},
"include_beans" : {
"type" : "Boolean",
"description" : "Do you want beans on your taco",
"required" : false,
"default" : false
},
"pounds" : {
"type" : "Double",
"description" : "How many pounds of meat do you want?",
"required" : false,
"default" : 0.1
},
"count" : {
"type" : "Integer",
"description" : "How many tacos would you like?",
"required" : false,
"default" : 0.0
}
}
请注意,每个属性都具有相同的结构。我最终想要的是Dictionary<string, TacoProperty>
,其中TacoProperty
定义为:
public class TacoProperty
{
public string type { get; set; }
public string description { get; set; }
public bool required { get; set; }
[JsonProperty(PropertyName = "default")]
public string defaultValue { get; set; }
public List<string> options { get; set; }
}
字典中的键应为“color”,“include_beans”等,所有TacoProperty
都应为值。
答案 0 :(得分:6)
Json.NET可以直接反序列化数据:
var tacoProperties =
JsonConvert.DeserializeObject<IDictionary<string, TacoProperty>>(json);