gpt4 book ai didi

c# - 使用 JSON.Net 反序列化 JSON Schema

转载 作者:行者123 更新时间:2023-12-03 19:39:49 28 4
gpt4 key购买 nike

我想使用 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 s 应该是值。

最佳答案

Json.NET可以直接反序列化数据:

var tacoProperties =
JsonConvert.DeserializeObject<IDictionary<string, TacoProperty>>(json);

关于c# - 使用 JSON.Net 反序列化 JSON Schema,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892942/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com