我正在尝试将Json数组中的数据集反序列化为C#对象。我在json中有50多个对象,不能使用JToken硬编码对象名称。 您能推荐任何解决方案吗? 谢谢
Data.json
{
"Luna": {
"Name": "Luna",
"Race": "Elf",
"Bonus": "Hunter"
},
"Axe": {
"Name": "Axe",
"Race": "Orc",
"Bonus": "Warrior"
}
}
Hero.cs
public class Hero
{
public string name { get; set; }
public string bonus { get; set; }
public string race { get; set; }
}
Program.cs
static void Main()
{
string json = File.ReadAllText("Data.json");
List<Hero> heroList = JsonConvert.DeserializeObject<List<Hero>>(json);
foreach(Hero hero in heroList)
{
Console.WriteLine(hero.name);
Console.WriteLine(hero.bonus);
Console.WriteLine(hero.race);
}
}