Im in a situation where I am receiving Json that can be in two different formats and I am trying to find a good way to deserialize it into their respective objects. I have one Json string that would look like:
{
"Id": 1,
"Result": "true",
"Comment": "test comment"
}
And the other like:
{
"UserId": 12,
"PhoneNumber": "123456789",
"Message": "test message"
}
I had created an object that could hold both structures, something like:
public class EnvelopeObject
{
public Structure1 { get; set; }
public Structure2 { get; set; }
}
and ideally after deserializing the json into EvelopeObject I would have one object populated with the data from the json and the other would just be null. I know that this situation would require a custom converter, but after reading the documentation and looking for other examples, Im unsure how to go about this. Is there a simple custom converter solution, or an easier way to go about this?
2
Answers
You could try and just deserialize both inner objects separately and set them to the Envelope Object. I believe that’s simple enough, no need for fancy custom conversion and deserialization.
Here is the code snippet of the
EnvelopeObject
class and two sub classes:With the
IsJson1Object()
method you can check which one is set.And portion of the code where the
EnvelopeObject
class is being created (with added example strings):i create you example as another approach. call function which will return tuple of 2 classes, that which will match – would be populated. i use ID (first property) to identify which class to bind
if you set jason as j2 or j1 you will get different class
the results would be
or