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; }
}