I have the following code:
var user = (Dictionary<string, object>)serializer.DeserializeObject(responsecontent);
The input in responsecontent
is JSON, but it is not properly parsed into an object. How should I properly deserialize it?
I have the following code:
var user = (Dictionary<string, object>)serializer.DeserializeObject(responsecontent);
The input in responsecontent
is JSON, but it is not properly parsed into an object. How should I properly deserialize it?
I think the best answer that I've seen has been @MD_Sayem_Ahmed.
Your question is "How can I parse Json with C#", but it seems like you are wanting to decode Json. If you are wanting to decode it, Ahmed's answer is good.
If you are trying to accomplish this in ASP.NET Web Api, the easiest way is to create a data transfer object that holds the data you want to assign:
You have simply add the application/json header to your request (if you are using Fiddler, for example). You would then use this in ASP.NET Web API as follows:
This helped me a lot when I was working in my Web Api and made my life super easy.
Another native solution to this, which doesn't require any 3rd party libraries but a reference to System.Web.Extensions is the JavaScriptSerializer. This is not a new but a very unknown built-in features there since 3.5.
..
and back
If .NET 4 is available to you, check out: http://visitmix.com/writings/the-rise-of-json (archive.org)
Here is a snippet from that site:
That last Console.WriteLine is pretty sweet...
You could also have a look at the DataContractJsonSerializer
As was answered here - Deserialize JSON into C# dynamic object?
It's pretty simple using Json.NET:
Or using Newtonsoft.Json.Linq :