I want to send a hashtable over a post request; for that I have to convert the hashtable to json.
I'm using Newtonsoft JSON.NET for that.
Hashtable ht = new Hashtable();
User u = new User();
u.name = "username";
u.last = "userlastname";
u.age = 30;
ht.Add("user1", u);
ht.Add("user2", u);
ht.Add("user3", u);
ht.Add("user4", u);
string value = JsonConvert.SerializeObject(ht);
The issue I have is that the value type (User
) is lost in when deserializing.
CodePudding user response:
You can use a TypeNameHandling option of Newtonsoft
var jsonSerializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All
};
string value = JsonConvert.SerializeObject(ht,jsonSerializerSettings);
ht=JsonConvert.DeserializeObject<Hashtable>(value,jsonSerializerSettings);