Angelo Sanchez
Angelo Sanchez

Reputation: 31

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.

Upvotes: 2

Views: 702

Answers (1)

arslanaybars
arslanaybars

Reputation: 1853

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.

Upvotes: 8

Related Questions