I have a class something like this
public class A{
public Guid userId { get; set; }
public Guid businessId { get; set; }
public Dictionary<int, long> rights { get; set; }
}
And I want to covert this json to this class
{
"userId": "dc2af693-72e1-49a7-80aa-6416c6536bdf",
"businessId": "0110eea4-7a47-4a7c-95ea-10547ab49652",
"rights": "{\"19\":1,\"17\":15,\"18\":1,\"23\":1,\"1\":31,\"20\":3,\"3\":1,\"16\":0}",
}
But when Im trying to convert it with NewtonSoft.Json
JsonConvert.DeserializeObject<A>(json);
I get the following error Cannot Cast System.string to System.Dictionary<int,long> What happens is that my rights get converted to a string something like this
"{"19":1, "17":15, "18":1, ..., "16":0}"
If I deserialize this string again it works as I desire is there better method to do this.
rightsis effectively a Json representation of a dictionary, so this could be a two-step process: first deserializerightsas a string, them deserialize it again as a dictionary:Dictionary<int, long> RightValues => JsonConvert.DeserializeObject<Dictionary<int, long>>(rights);rightsproperty with a custom JsonConverter - in the customJsonConverteryou'll be able to deserialize the string and then convert into a dictionary.