Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm programming a C# implementation for the Qualtrics API (v 2.5) using RestSharp. When calling the method getUserIds, it returns a list of users in JSON format (see the example output below).

The problem/question I face is that for each user object (the list of objects under Result) it generates a different id, starting with URH_. When using json2csharp it assumes ofcourse that it's always a different class, while in fact it's absolutely the same one as you can see in the output, and as is stated in the documentation of the api. How can I best resolve this - so that I can make a class UserData that I can reuse? Because now I obviously always see these random URH_ prefixed classes in each response.

NOTE: I was thinking I could try to massage the response first, and when I get the response replace each URH_ prefixed object under the root Result object with a "UserData" string - but I feel this is breaking the rules a bit, and thought the community would have a better solution?


Below is the raw JSON output (note that I removed sensitive information):

{"Meta":{"Status":"Success","Debug":""},"Result":{"URH_3wpA9pxGbE0c7Xu":{"DivisionID":null,"UserName":"[email protected]","UserFirstName":"x","UserLastName":"x","UserAccountType":"UT_4SjjZmbPphZGKDq","UserEmail":"[email protected]","UserAccountStatus":"Active"},"URH_57vQr8MVXgpcPUo":{"DivisionID":"DV_XXXXXXXX","UserName":"[email protected]","UserFirstName":"X","UserLastName":"X","UserAccountType":"UT_BRANDADMIN","UserEmail":"[email protected]","UserAccountStatus":"Active"},"URH_6ujW1EP0QJOUaoI":{"DivisionID":"DV_XXXXXXXYZ","UserName":"[email protected]","UserFirstName":"x","UserLastName":"x","UserAccountType":"UT_XXXXXABCD","UserEmail":"[email protected]","UserAccountStatus":"Active"}}}

This is what I get when generating 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; }
}
like image 290
Yves Schelpe Avatar asked Mar 15 '23 03:03

Yves Schelpe


1 Answers

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

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; }
}
like image 197
rufanov Avatar answered Apr 26 '23 19:04

rufanov