将许多相同细节的结果集组合成一个Newtonsoft.Json



我想知道是否有人能帮我。我正在查询一个API,它给了我如下的响应:

{
"clients": {
"one": {
"id": "iphone6p_9",
"category": "Mobile"
},
"two": {
"id": "gmail_chr26_win",
"category": "Web"
},
"three": {
"id": "outlook16",
"category": "Application"
}
}
}

在c类中,哪个会在下面

public class One
{
public string id { get; set; }
public string category { get; set; }
}
public class Two
{
public string id { get; set; }
public string category { get; set; }
}
public class Three
{
public string id { get; set; }
public string category { get; set; }
}
public class Clients
{
public One one { get; set; }
public Two two { get; set; }
public Three three { get; set; }
}
public class Root
{
public Clients clients { get; set; }
}

是否可以使用Newtonsoft.Json将One、Two和Three动态地放入同一类型,因为它们是相同的,而不是单独的x次。像列表

您可以使用以下代码:

public class Client
{
public string Id { get; set; }
public string Category { get; set; }
}
public class Clients
{
public Client One { get; set; }
public Client Two { get; set; }
public Client Three { get; set; }
}
public class Root
{
public Clients Clients { get; set; }
}

和反序列化:

var clients = JsonConvert.DeserializeObject<Root>(*jsonStringHere*); 

最新更新