实例如下:
{
"employees":[
{"firstName":"Bill","lastName":"Gates"},
{"firstName":"George","lastName":"Bush"},
{"firstName":"Thomas","lastName":"Carter"}
]
}
下面以Json文件的读取为例
Skill.txt
{
"name":"扁鹊",
"maxHP":2000,
"type":"法师",
"skillList":[
{"id":2,"name":"技能1","damage":200},
{"id":3,"name":"技能2","damage":255},
{"id":4,"name":"技能3","damage":300}
]
}
Skill.cs
internal class Skill
{
public int id { get; set; }
public string name { get; set; }
public int damage { get; set; }
}
Hero.cs
internal class Hero
{
public string name { get; set; }
public int maxHP { get; set; }
public string type { get; set; }
public List<Skill> skillList { get; set; }
}
NuGet按照Json包
主要处理逻辑
internal class Program
{
static void Main(string[] args)
{
Hero hero = JsonConvert.DeserializeObject<Hero>(File.ReadAllText("Hero.txt"));
Console.WriteLine(hero.name + "," + hero.type + "," + hero.maxHP);
foreach (Skill s in hero.skillList)
{
Console.WriteLine(s.id + ", " + s.name + " ," + s.damage);
}
Console.ReadKey();
}
}