Vijay
Vijay

Reputation: 17

How can we create an anonymous object from json string in c#?

My JSON string will be like

enter image description here

when I deserialize it using Newtonsoft.Json, instead of object I am getting below response

enter image description here

But it should be like below image

enter image description here

tried JsonConvert.DeserializeObject and JObject.Parse. is there any way to get in direct object structure when we deserialize it without knowing type?

Upvotes: -1

Views: 2914

Answers (1)

David Fox
David Fox

Reputation: 10753

Try a dynamic? e.g.

dynamic thing = JObject.Parse("{Id:123, PhoneNumber: { Primary: 12345, Secondary: 78945}");

Console.WriteLine(thing.Id);
Console.WriteLine(thing.PhoneNumber);
Console.WriteLine(thing.PhoneNumber.Primary);

Upvotes: 2

Related Questions