Here is a snippet of JSON that I need to deserialize into objects, do some limited changes to the objects, and then serialize back to JSON for submitting to a server. To me, the problem is the format of the JSON. But that format won’t be changed.
"{\"SourceID\":\"4\",\"Machines\":{\"2017\":{\"Machine\":{\"Version\":\"501\"}}}}";
2017 is the ID of the machine. So, yeah, it would be much better if they would just update the JSON to reflect that and have it be a key value pair inside the Machine class instead of using the ID as the class type, but they won’t do that. I changed it myself to reflect the change, and then I could create sensible POCOs, and FromJson() and ToJson() work wonderfully.
I don’t want to have a class for every ID. I looked into deserializing to a Dictionary<string,object> and that sort of works, but that is a lot of brittle code, especially given that the schema of the real JSON is more of the same.
Any ideas on how to best approach this?