how to decode a json response in c#?
-
"the array" : which array are you talking about ? – Thomas Levesque Aug 26 '09 at 12:56
-
Also check out http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object one of the examples towards the bottom references `Json.Decode()` from `System.Web.Helpers` which I've used successfully in the past. – Luis Perez Oct 26 '13 at 21:18
-
Closing a question, after 4 years, wait... this is SO. – Sahar Ch. Jul 21 '14 at 13:26
4 Answers
Check out the DataContractJsonSerializer. You'll have to target .NET 3.5, which means Visual Studio 2008 is pretty much required. Here's a good blog post about using the Json data contract serializer.
-
2
-
1The [.Net 4 version of DataContractJsonSerializer](http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx) is available. – MrBry Jan 23 '12 at 14:04
- 62,844
- 17
- 138
- 209
-
I was typing check out but I mistyped and then did the whole line again. lol – Preet Sangha Aug 26 '09 at 13:00
-
1thank you all.. im not that much familier with .net and need to use a json data . So it will be greatful if anyone can give a code sample stating how to decode the the json data – raki Aug 26 '09 at 19:11
In addition to the 3.5 methods above, if you install the ASP.NET 2.0 AJAX Extensions 1.0 (2.0 is the framework version), you will gain the System.Web.Script.Serialization.JavaScriptSerializer class, which can encode/decode json.
- 84,782
- 17
- 123
- 168
The .NET integrated classes have their merits. But they have their shortcomings.
For example, DataContractJsonSerializer is not available in .NET 2.0, System.Web.Extensions needs admin rights to install it (in NET 2.0 - you can localcopy it, if you don't have a WebSite project) plus it doesn't work in SilverLight and WindowsPhone. If you have a WebSite project, you need to copy the System.Web.Extensions assemblies to your project, and remove them from GAC afterwards, else VisualStudio doesn't understand it has to localcopy them.
But more importantly, if you work with pretty much any JavaScript library, e.g. SlickGrid (AJAX grid), you will stumble upon this valid JavaScript object (but it's invalid JSON, because fnFormatDate_DE is a function call and not text, it lacks the quotation marks):
FormatterCallback :
{
name : "DateFormatter_DE"
func: fnFormatDate_DE(val)
}
No chance to serialize this with any of the .NET integrated classes (because it's invalid JSON). Also, they fall short in terms of performance, availability in SilverLight, Windows Phone and WindowsRT. They are neither OpenSource nor MIT license. They have no support for indentation (human readable JSON), and they can't serialize DataTables, and they have problems with circular references. You can't handle serialization errors with them, can't serialize enums to their names, and you can't switch the date format (OK, this is not really a problem, because the MS date format is the only date format the safari crap understands [it doesn't undestand ISO]), and they don't serialize neither nHibernate nor Entity...
But most importantly, you won't want to switch your library or adjust project references if you go from .NET 2.0 to 4.0, you don't want to rewrite your code if you want to use some code in SilverLight/Windows Phone, and you don't want to be write a function to beautify JSON if you want to look whether you got the class right, and you won't want to write your own method to strip out quotation marks just because Microsoft's libraries can't handle invalid JSON.
Also, Microsoft's libraries have a low performance, and they can't serialize to BSON (for use with NoSQL databases like MongoDB).
So for all these reasons, you better choose NewtonSoft JSON (JSON.NET).
It's free and OpenSource (MIT license, not GPL).
There is a nice comparison matrix here:
http://james.newtonking.com/pages/json-net.aspx
- 73,615
- 63
- 359
- 429
-
Going by: https://github.com/dotnet/corefx-progress/blob/master/src-diff/README.md, `System.Runtime.Serialization.Json` will be open-sourced and available in corefx repo: https://github.com/dotnet/corefx (under MIT license, like all other .NET libraries landed there). – vulcan raven Mar 12 '15 at 17:59