21

How can I decode a json response in C#?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raki
  • 2,253
  • 8
  • 33
  • 42
  • "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 Answers4

21

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    The [.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
5

See here for info on the DataContractJsonSerializer

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
5

In addition to the 3.5 methods in user1228's answer, 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Powerlord
  • 87,612
  • 17
  • 125
  • 175
3

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 Windows Phone. If you have a WebSite project, you need to copy the System.Web.Extensions assemblies to your project, and remove them from the GAC afterwards, else Visual Studio 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)
}

There isn't any 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 Windows RT. They are neither open source nor MIT License. They don't have any 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 Microsoft date format is the only date format the Safari crap understands (it doesn't understand ISO 8601)), and they don't serialize neither NHibernate nor Entity Framework...

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 open source (MIT License, not GPL).
There is a nice comparison matrix here:
http://james.newtonking.com/pages/json-net.aspx

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
  • 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
  • The `james.newtonking.com` link is (effectively) broken. It redirects to a generic page. – Peter Mortensen Jul 10 '23 at 13:24