My web-api
returns an User Object. In that object there is a DateTime
property. When i'am reading it in my Application i get an error because the string that would represent the DateTime isn't valid it's missing Date ...
{System.Runtime.Serialization.SerializationException: There was an
error deserializing the object of type User. DateTime content
'1984-10-02T01:00:00' does not start with '/Date(' and end with ')/'
as required for JSON. --->
public static async Task<User> GetUser(string email)
{
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url + "?email="+email);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
User user = DataService.Deserialize<User>(content);
return user;
}
return null;
}
}
catch (Exception ex)
{
return null;
}
}
This is the method i use to deserialize.
public static T Deserialize<T>(string json) {
try
{
var _Bytes = Encoding.Unicode.GetBytes(json);
using (MemoryStream _Stream = new MemoryStream(_Bytes))
{
var _Serializer = new DataContractJsonSerializer(typeof(T));
return (T)_Serializer.ReadObject(_Stream);
}
}
catch (Exception ex)
{
throw ex;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…