Saturday, 20 October 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...