33

What's the best way to parse JSON data into a .NET object? I am trying to assist a coder friend of mine and he is trying to dump some data from a JSON string into a database using ASP.net. Are there any prebuilt scripts that would make this happen?

Thanks in advance to any help.

Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
mhipp
  • 331
  • 1
  • 3
  • 3

4 Answers4

22

The .NET Framework 3.5 has the JavaScriptSerializer class that can ease the deserialization. You can also use third party libraries like JSON.NET.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
14

Actually you should really look at the DataContractJsonSerializer as the JavaScriptSerializer was listed as Obsolete in the .NET 3.5 framework.

Admittedly ScottGu stated that it may have been a mistake and it may be reinstated in the future.

Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
  • 20
    FYI, JavaScriptSerializer was undepricated in .NET 3.5 SP1. http://www.danrigsby.com/blog/index.php/2008/05/28/javascriptserializer-undeprecated-in-net-35-sp1/ – chrish May 20 '11 at 11:21
2

If you are using .NET 3.5, you probably don't need a third party library. The JavaScriptSerializer class can be used (just repeating what was mentioned before) but you also have access to the DataContractJsonSerializer, which offers a different model for mapping between CLR objects and JSON.

casperOne
  • 73,706
  • 19
  • 184
  • 253
0

Arguably the fastest way is to use JSON#, which avoids reflection and the associated performance overhead - this can be significant in web applications. It also gives you much more control in terms of the parsing process itself.

Paul Mooney
  • 1,576
  • 12
  • 28