lundi 15 avril 2019

Convert a JSON object/string to a C# dynamic object and have access to the properties/values?

I am trying to take a huge JSON file (could be a string) and without knowing the actual structure of the data I want to read and process it as a class in C#. I tried using JSON to deserialize it but I wasn't totally sure about where to go after that. I was thinking of using Reflections but not sure what data I need.

I have tried to deserialize the object as the code shows. But I want to test if it is the right object type incase it isn't I would hope it fails but I can't seem to get past this part. I also am not sure what to do with reflections inside of the check. I know I should iterate but not sure which property values inside of the object will contain what I need.

string jsonData = sr.ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(jsonData);
if (data is List<dynamic>)
{
    data.GetType().GetProperties();
}

I want an object that has all the access to the data from a JSON file/string.





Aucun commentaire:

Enregistrer un commentaire