RESTApi为同一对象返回不同的对象名称,如何使用RestSharp进行处理
本文关键字:对象 何使用 RestSharp 处理 返回 RESTApi | 更新日期: 2023-09-27 18:22:21
我正在使用RestSharp为Qualtrics API(v 2.5)编程一个C#实现。当调用方法getUserIds时,它以JSON格式返回一个用户列表(请参阅下面的示例输出)。
我面临的问题是对于每个用户对象(Result
下的对象列表),它会从URH_
开始生成不同的id。当使用json2csharp时,它当然会假设它总是一个不同的类,而事实上,正如您在输出中看到的那样,它绝对是同一个类,正如api文档中所述我如何最好地解决这个问题,这样我就可以创建一个可以重用的类UserData
因为现在我显然总是在每个响应中看到这些随机的URH_
前缀类。
注意:我想我可以先尝试按摩响应,当我得到响应时,用"UserData"
字符串替换根Result
对象下的每个前缀为URH_
的对象,但我觉得这有点违反规则,我认为社区会有更好的解决方案?
下面是原始JSON输出(注意我删除了敏感信息):
{"Meta":{"Status":"Success","Debug":""},"Result":{"URH_3wpA9pxGbE0c7Xu":{"DivisionID":null,"UserName":"user.name@domain.com","UserFirstName":"x","UserLastName":"x","UserAccountType":"UT_4SjjZmbPphZGKDq","UserEmail":"x.x@x.x","UserAccountStatus":"Active"},"URH_57vQr8MVXgpcPUo":{"DivisionID":"DV_XXXXXXXX","UserName":"jxxxx@xx.xxx","UserFirstName":"X","UserLastName":"X","UserAccountType":"UT_BRANDADMIN","UserEmail":"xxxx@xxg.xxx","UserAccountStatus":"Active"},"URH_6ujW1EP0QJOUaoI":{"DivisionID":"DV_XXXXXXXYZ","UserName":"x.xckx@xxx.xyz","UserFirstName":"x","UserLastName":"x","UserAccountType":"UT_XXXXXABCD","UserEmail":"c.c@cc.com","UserAccountStatus":"Active"}}}
这是我使用json2csharp生成模型时得到的结果:
public class Meta
{
public string Status { get; set; }
public string Debug { get; set; }
}
public class URH3wpA9pxGbE0c7Xu
{
public object DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}
public class URH57vQr8MVXgpcPUo
{
public string DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}
public class URH6ujW1EP0QJOUaoI
{
public string DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}
public class Result
{
public URH3wpA9pxGbE0c7Xu URH_3wpA9pxGbE0c7Xu { get; set; }
public URH57vQr8MVXgpcPUo URH_57vQr8MVXgpcPUo { get; set; }
public URH6ujW1EP0QJOUaoI URH_6ujW1EP0QJOUaoI { get; set; }
}
public class RootObject
{
public Meta Meta { get; set; }
public Result Result { get; set; }
}
这很简单-只需对Result
字段使用Dictionary<string, UserData>
泛型类型:
public class Response
{
public Meta Meta { get; set; }
public Dictionary<string, UserData> Result { get; set; }
}
public class Meta
{
public string Status { get; set; }
public string Debug { get; set; }
}
public class UserData
{
public string DivisionID { get; set; }
public string UserName { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserAccountType { get; set; }
public string UserEmail { get; set; }
public string UserAccountStatus { get; set; }
}