我正在与外部合作伙伴提供的Json作斗争:
{
"info": {
"compa": "123"
},
"employees": {
"key1": {
"name": "dog",
"friend": [ "cat" ]
},
"key2": {
"name": "fish",
"friend": [ "shark" ]
}
}
}
我使用类:
public class company
{
public info info { get; set; }
public employees employees { get; set; }
}
public class info
{
public string compa { get; set; }
}
public class employees
{
public List<Dictionary<string, employee>> employee { get; set; }
}
public class employee
{
public string name { get; set; }
public string friend { get; set; }
}
反序列化时,employees类中的employee属性为空。
任何帮助都将不胜感激!
您的C#模式存在一些问题。您可以去掉employees
类,并将其替换为Dictionary<string, employee>
。请注意,这是一本字典,而不是字典列表。还要注意,employee.friend
是一个集合,而不是字符串。
这项工作:
class Program
{
static async Task Main(string[] args)
{
string json = await File.ReadAllTextAsync("json1.json");
var company = JsonSerializer.Deserialize<company>(json);
}
}
public class company
{
public info info { get; set; }
public Dictionary<string, employee> employees { get; set; }
}
public class info
{
public string compa { get; set; }
}
public class employee
{
public string name { get; set; }
public IEnumerable<string> friend { get; set; }
}