Saturday, October 20, 2018

.net - How to deserialize json to multiple types dynamically in c#





I have a json string like below :



{
"error" : "xxxx" ,
"data" : int type or {...other complex json object}
}



I don't know when return int or other json string from data field , and how to deserialize this ?



when type of data is object or dynamic , the real deserialized type of data is int or Dictionary using JavaScriptSerializer.Deserialize(string json).




the real deserialized type of data is Int64 or JContainer when using Json.Net.



is there a elegance way to solve this ? I don't think reflection is a good way.


Answer



You can create object-wrapper and put data type identificator to the first field of this wrapper.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...