21

How can I decode a json response in C#?

3
  • "the array" : which array are you talking about ? Commented Aug 26, 2009 at 12:56
  • Also check out stackoverflow.com/questions/2246694/… one of the examples towards the bottom references Json.Decode() from System.Web.Helpers which I've used successfully in the past. Commented Oct 26, 2013 at 21:18
  • Closing a question, after 4 years, wait... this is SO. Commented Jul 21, 2014 at 13:26

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

5

See here for info on the DataContractJsonSerializer

Comments

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.

Comments

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

2 Comments

Going by: github.com/dotnet/corefx-progress/blob/master/src-diff/…, System.Runtime.Serialization.Json will be open-sourced and available in corefx repo: github.com/dotnet/corefx (under MIT license, like all other .NET libraries landed there).
The james.newtonking.com link is (effectively) broken. It redirects to a generic page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.