I want to deserialize a json object in c# using JsonSerializer.Deserialize of System.Text.Json. The json looks like this:
{"id":10,"authorization_ids":[],"karma_user_ids":[2],"group_ids":{"2":["full"],"4":["read"],"5":["overview"],"7":["change","overview"],"10":["create"]}}
But the property "group_ids" contains objects with dynamic property names that actually correspond to the ID of a group.
{
2 : {full}
4 : {read}
5 : {overview}
7 : {change, overview}
10 : {create}
}
Now I want to deserialize group_ids to an object like this:
public class GroupPermission
{
public int GroupID { get; set; }
public Permission Permission { get; set; }
public GroupPermission() { }
}
[Flags]
public enum Permission
{
full = 1,
read = 2,
overview = 4,
change = 8,
create = 16
}
Is this possible? Can someone point me the right direction?
Regards
Should be something like this:
public class RootObject
{
[JsonPropertyName("id")]
public int Id {get;set;}
[JsonPropertyName("authorization_ids")]
public List<int> AuthorizationIds {get;set;}
[JsonPropertyName("karma_user_ids")]
public List<int> KarmaUserIds {get;set;}
[JsonPropertyName("group_ids")]
public Dictionary<int, List<string>> GroupIds {get;set;}
public RootObject()
{
AuthorizationIds = new List<int>();
KarmaUserIds = new List<int>();
GroupIds = new Dictionary<int, List<string>>();
}
}
To test it:
public static void Main(string[] args)
{
var json = File.ReadAllText("./file.json");
var objs = JsonSerializer.Deserialize<RootObject>(json);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.