-3

I am using the DataContractJsonSerializer in C# and I am trying to deserialize the below Json array, how ever it is failing because the "PlaceName" string value contains a comma.

Error message:

XmlException: The value 'Chicago, IL' cannot be parsed as the type 'Int32'.

[{\"PlaceId\":343,\"PlaceName\":\"Chicago, IL\"},{\"PlaceId\":345,\"PlaceName\":\"New York, NY\"}]

This is the function I am using.

deserializeJSON function

static public T deserializeJSON<T>(string json)
{
    var instance = typeof(T);

    using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
    {
        var deserializer = new DataContractJsonSerializer(instance);
        return (T)deserializer.ReadObject(ms);
    }
}
string json = "[{\"PlaceId\":343,\"PlaceName\":\"Chicago, IL\"},{\"PlaceId\":345,\"PlaceName\":\"New York, NY\"}]"
List<Places> places = new List<Places>();

places = Common.deserializeJSON<List<Places>>(json);

Class

public class Places
{
    public int PlaceId { get; set; }
    public int PlaceName { get; set; }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
davey1971
  • 11
  • 1

1 Answers1

4

Your problem has nothing to do with the comma, but with the class you provided. PlaceName should be a string, not int.

Blindy
  • 65,249
  • 10
  • 91
  • 131