UrsulRosu UrsulRosu -4 years ago 107
C# Question

Generic class to hold JSON data of gerrit

The problem is that the gerrit response for getting all groups looks like this, and I find that having the name of the group outside of the {} creates problems for me because when I create a class to hold the group, I am forced to have the class named like the group to be able to deserialize the response.

But whenever a new gerrit group is created I don't want to create a new class with that name.

[{
"Administrators": {
"id": "6a1e70e1a88782771a91808c8af9bbb7a9871389",
"url": "#/admin/groups/uuid-6a1e70e1a88782771a91808c8af9bbb7a9871389",
"options": {
},
"description": "Gerrit Site Administrators",
"group_id": 1,
"owner": "Administrators",
"owner_id": "6a1e70e1a88782771a91808c8af9bbb7a9871389"
},
"Anonymous Users": {
"id": "global%3AAnonymous-Users",
"url": "#/admin/groups/uuid-global%3AAnonymous-Users",
"options": {
},
"description": "Any user, signed-in or not",
"group_id": 2,
"owner": "Administrators",
"owner_id": "6a1e70e1a88782771a91808c8af9bbb7a9871389"
},
"MyProject_Committers": {
"id": "834ec36dd5e0ed21a2ff5d7e2255da082d63bbd7",
"url": "#/admin/groups/uuid-834ec36dd5e0ed21a2ff5d7e2255da082d63bbd7",
"options": {
"visible_to_all": true,
},
"group_id": 6,
"owner": "MyProject_Committers",
"owner_id": "834ec36dd5e0ed21a2ff5d7e2255da082d63bbd7"
}]


Here are the classes:

public class Administrators
{
public string id { get; set; }
public string url { get; set; }
public Options options { get; set; }
public string description { get; set; }
public int group_id { get; set; }
public string owner { get; set; }
public string owner_id { get; set; }
}

public class AnonymousUsers
{
public string id { get; set; }
public string url { get; set; }
public options { get; set; }
public string description { get; set; }
public int group_id { get; set; }
public string owner { get; set; }
public string owner_id { get; set; }
}

public class MyProjectCommitters
{
public string id { get; set; }
public string url { get; set; }
public options { get; set; }
public int group_id { get; set; }
public string owner { get; set; }
public string owner_id { get; set; }
}


and what I want to do is have a class called GerritGroup to hold all this and to be able to deserialize any such group in an object.

The way I do it now is like this:

IList<GerritGroup> grps = JsonConvert.DeserializeObject<IList<GerritGroup>>(cleanedResponse);


and I get a list with one object with all properties null.

Can you give me some advice regarding this JSON response?

Answer Source

You've got a collection that's holding an object with key-value pairs, where the keys are variable and the values are groups.

In other words, you only need one Group class:

public class Group
{
    public string id { get; set; }
    public string url { get; set; }
    public Dictionary<string, string> options { get; set; }
    public string description { get; set; }
    public int group_id { get; set; }
    public string owner { get; set; }
    public string owner_id { get; set; }
}

And you need to deserialize your JSON as a List<Dictionary<string, Group>>:

var groups = JsonConvert.DeserializeObject<List<Dictionary<string, Group>>>(json);

foreach (var g in groups)
{
    foreach (var e in g)
    {
        Console.WriteLine(e.Key + " " + e.Value.description);
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download