1

Hi I am getting a response from server which is something like this

{"total":110,"responses":{"13":26,"14":24,"15":40,"16":20}}

I am not able to deserialize it using DataContractJSONSerializer. I have tried various combination for it but no results. I am using following class for de-serializing:

public class PollResponseRoot
{
    public int total { get; set; }
    public Dictionary<int, int> Responses;

}

but I always get the Response value null. Please let me know where I am going wrong.

Avijeet
  • 365
  • 4
  • 14
  • The JSON string is legitimate JSON. – Jonathan M Nov 18 '11 at 06:50
  • what does that mean, excuse me for my naiveness i am new to .net and wp7 – Avijeet Nov 18 '11 at 06:54
  • I'm just letting you know the problem is not in the format of the JSON string. – Jonathan M Nov 18 '11 at 06:55
  • yeah, thanks for that, strange thing if i change the name of the Dictionary in my class from Responses to responses , i get argument exception. while De-serializing – Avijeet Nov 18 '11 at 06:59
  • How about public Dictionary responses; Oh and mark the class as a DataContractAttribute and the properties as DataMember. You could also specify [DataMember(Name="responses")] to leave the property name properly PascalCased. – Filip Skakun Nov 18 '11 at 07:15

2 Answers2

2

Dictionaries are not supported by DataContractJsonSerializer, you could try your luck with Json.Net instead.

Also available on NuGet: http://nuget.org/List/Packages/Newtonsoft.Json

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • Bendik json.net is available for windows phone 7 ?, i tried including the .net3.5 binaries and got a warning that "including this lib can have unexpected application behavior" – Avijeet Nov 18 '11 at 07:28
  • Json.net should be very much available on wp7, i have used on several occasions, but you need the 4.0 version or you could just add it to the project by using NuGet – Bendik Nilsen Aune Nov 18 '11 at 07:44
2

See my explanation on a similar question why it's not possible to decode such lists using DataContractJsonSerializer. Use Json.NET instead.

Community
  • 1
  • 1
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85