如何使用 restsharp 反序列化此 JSON 格式? (C#)
How can I deserialize this JSON format using restsharp? (c#)
我的 类 应该是什么样子,以便我可以反序列化下面的 JSON。
{
"Bob": [{
"code": "Bob",
"tier": 1
},
{
"code": "Bob",
"tier": 2
}
],
"Joe": [{
"code": "Joe",
"tier": 1
},
{
"code": "Joe",
"tier": 2
}
]
}
下面的 类(来自 json2csharp.com)有效,但是当我的 json 有很多不同的名字时,这不会扩展,因为我最终会得到 100+ 类.
public class Bob
{
public string code { get; set; }
public int tier { get; set; }
}
public class Joe
{
public string code { get; set; }
public int tier { get; set; }
}
public class RootObject
{
public List<Bob> Bob { get; set; }
public List<Joe> Joe { get; set; }
}
任何建议或想法都会对我有帮助。(我试过使用字典和动态列表)
如果您不想要 JSON 的结构,那么您可以尝试使用字典而不是根据 JSON [=21= 命名的 类 ] 名字。例如
public class Person {
public string code {get; set; }
public int tier {get; set; }
}
并反序列化 JSON 不是 RootObject
而是 Dictionary<string, List<Person>>
我的 类 应该是什么样子,以便我可以反序列化下面的 JSON。
{
"Bob": [{
"code": "Bob",
"tier": 1
},
{
"code": "Bob",
"tier": 2
}
],
"Joe": [{
"code": "Joe",
"tier": 1
},
{
"code": "Joe",
"tier": 2
}
]
}
下面的 类(来自 json2csharp.com)有效,但是当我的 json 有很多不同的名字时,这不会扩展,因为我最终会得到 100+ 类.
public class Bob
{
public string code { get; set; }
public int tier { get; set; }
}
public class Joe
{
public string code { get; set; }
public int tier { get; set; }
}
public class RootObject
{
public List<Bob> Bob { get; set; }
public List<Joe> Joe { get; set; }
}
任何建议或想法都会对我有帮助。(我试过使用字典和动态列表)
如果您不想要 JSON 的结构,那么您可以尝试使用字典而不是根据 JSON [=21= 命名的 类 ] 名字。例如
public class Person {
public string code {get; set; }
public int tier {get; set; }
}
并反序列化 JSON 不是 RootObject
而是 Dictionary<string, List<Person>>