Is there a way to deserialize JSON content into a C# 4 dynamic type? It would be nice to skip creating a bunch of classes in order to use the DataContractJsonSerializer.
相关问题
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- Graphics.DrawImage() - Throws out of memory except
- StackExchange API - Deserialize Date in JSON Respo
I made a new version of the DynamicJsonConverter that uses Expando Objects. I used expando objects because I wanted to Serialize the dynamic back into json using Json.net.
You can use
using Newtonsoft.Json
resolvedEvent.Event.Data
is my response getting from calling core Event .Look at the article I wrote on CodeProject, one that answers the question precisely:
Dynamic types with JSON.NET
There is way too much for re-posting it all here, and even less point since that article has an attachment with the key/required source file.
How to parse easy json with dynamic & JavaScriptSerializer
Please add reference of System.Web.Extensions and add this namespace
using System.Web.Script.Serialization;
at topHow to parse nested & complex json with dynamic & JavaScriptSerializer
Please add reference of System.Web.Extensions and add this namespace
using System.Web.Script.Serialization;
at topIt's pretty simple using Json.NET:
Also
using Newtonsoft.Json.Linq
:Documentation: Querying JSON with dynamic
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: