1

Possible Duplicate:
Deserializing JSON into an object with Json.NET

Any ideas of how to deserialize the following response into anonymous type using Json.Net?

{"d":[{"day":"Wednesday","firstSet":{"start":"17:00","close":"23:00","hours":6,"isValid":true},"secondSet":{"start":"00:00","close":"00:00","hours":0,"isValid":false},"personMinimum":2,"personMaximum":25}]}

Attempts so far result in the following error

Could not cast or convert from System.String to <>f__AnonymousType35[System.String,<>f__AnonymousType24[System.String,System.String,System.Int32,System.Boolean],<>f__AnonymousType2`4[System.String,System.String,System.Int32,System.Boolean],System.Int32,System.Int32].

Code is supplied below

var json_complex = new
{
  d = new
  {
    day = "",
    firstSet = new
    {
      start = "",
      close = "",
      hours = 0,
      isValid = false
    },
    secondSet = new
    {
      start = "",
      close = "",
      hours = 0,
      isValid = false
    },
    personMinimum = 2,
    personMaximum = 25
  }
};
var json = JsonConvert.DeserializeAnonymousType(jsonResponse, json_complex);

Any ideas?

Community
  • 1
  • 1
Sean Dooley
  • 615
  • 2
  • 11
  • 27
  • See stackoverflow [post][1] about deserializing JSON into an object with Json.NET for the answer [1]: http://stackoverflow.com/questions/2555363/deserializing-json-into-an-object-with-json-net/2555899#2555899 – Sean Dooley Jun 21 '11 at 12:22

1 Answers1

-10

First of all I must say why are you including third party into your application ?

Asp.net provides JavaScriptSerializer for serialization !

Look at my question and refer the answer you can get idea from this

Community
  • 1
  • 1
Chintan
  • 2,768
  • 4
  • 27
  • 43
  • 3
    It can be because Json.net is more convenient and flexible than JavaScriptSerializer. E.g. Json.net has a linq-2-json feature which allows you to build Json object "on the fly" and query Json objects using linq syntax. – Dmitrii Lobanov Feb 15 '12 at 11:21
  • 3
    "Why are you including third party into your application ?" - Why are you against using awesome opensource code? – Mikey G Mar 20 '13 at 19:45
  • Don't use JavaScriptSerializer. Use Newtonsoft.Json. Even Microsoft use it rather than their own System.Json. JavaScriptSerializer has a lot of problems that Newtonsoft will overcome. Plus Newtonsoft is faster. – darksider474 Dec 03 '14 at 23:35