REST Api returns different object names for the same object, how to handle RestSharp?

I am programming a C # implementation for the Qualtrics API (v 2.5) using RestSharp . When the getUserIds method is called, it returns a list of users in JSON format (see the Example below).

The task / question that I am facing is that for each user object (list of objects under Result), it generates a different identifier, starting with URH_. When using json2csharp, it assumes that it is always a different class, although in fact it is exactly the same as you can see in the output, and as indicated in the api documentation. How can I best resolve this so that I can use a class UserDatathat I can reuse? . Because now I always see these random URH_prefix classes in every answer.

NOTE. I thought that I would try to massage the answer first, and when I get the answer, replace each URH_prefix object under the root object Resultwith a string "UserData", but I feel this violation of the rule is a bit and thought the community would have a better solution?


Below is the original JSON output (note that I deleted the confidential information):

{"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"}}}

This is what I get when creating a model using 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; }
}
+4
source share
1 answer

It's simple - just use a Dictionary<string, UserData>generic type for the field Result:

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; }
}
+7
source

Source: https://habr.com/ru/post/1616174/


All Articles