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

2.6k Views Asked by At

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?

1

There are 1 best solutions below

0
David Fox On

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);