Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically deserialize from JSON

I have some JSON here. The problem is it doesn't match the classes data types anymore. My question is; is it possible to deserialize JSON Dynamically? i.e. if I have entirely different JSON's can I deserialize them into two entirely different classes without first knowing what class I want to deserialize each into.

like image 729
Angelo Sanchez Avatar asked Dec 15 '25 02:12

Angelo Sanchez


1 Answers

You can deserialize dynamic object with using newtonsoft

like bellowing code piece.

 dynamic dynamicObj = JsonConvert.DeserializeObject(jsonStr);
 string name = dynamicObj.data.code;

But in my personal preference is using strong type. I think its more convenience.

you can use quictype for generating c# classes from JSON object

quicktype generates strongly-typed models and serializers from JSON, JSON Schema, and GraphQL queries, making it a breeze to work with JSON type-safely in any programming language.

Hope the answer helps to you.

like image 177
arslanaybars Avatar answered Dec 16 '25 14:12

arslanaybars



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!