Is there a way to deserialize JSON content into a C# dynamic type? It would be nice to skip creating a bunch of classes in order to use the DataContractJsonSerializer
.
C# – Deserialize JSON into C# dynamic object
cdynamicjsonnetserialization
Related Topic
- Json – How to pretty-print JSON in a shell script
- Json – the correct JSON content type
- Json – How to deserialize JSON to a simple Dictionary
in ASP.NET - C# – JavaScriptSerializer – JSON serialization of enum as string
- Javascript – Why does Google prepend while(1); to their JSON responses
- Javascript – pretty-print JSON using JavaScript
- C# – How to turn a C# object into a JSON string in .NET
- Json – How to POST JSON data with cURL
Best Answer
If you are happy to have a dependency upon the
System.Web.Helpers
assembly, then you can use theJson
class:It is included with the MVC framework as an additional download to the .NET 4 framework. Be sure to give Vlad an upvote if that's helpful! However if you cannot assume the client environment includes this DLL, then read on.
An alternative deserialisation approach is suggested here. I modified the code slightly to fix a bug and suit my coding style. All you need is this code and a reference to
System.Web.Extensions
from your project:You can use it like this:
So, given a JSON string:
The following code will work at runtime: