-1

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?

Vijay
  • 17
  • 1
  • 5
  • 1
    Possible duplicate of [Deserialize json object into dynamic object using Json.net](https://stackoverflow.com/q/4535840/8395242) – Andrew Feb 20 '20 at 14:55
  • Does this answer your question? [Deserialize json object into dynamic object using Json.net](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) – Andrew Feb 20 '20 at 14:56
  • Whenever we deserialize to object/dynamic, we are getting object but it is not exactly as we create an anonymous object, it is in different structure. – Vijay Feb 24 '20 at 10:31

1 Answers1

2

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);
David Fox
  • 10,603
  • 9
  • 50
  • 80