【发布时间】:2012-12-26 16:38:53
【问题描述】:
我的web-api 返回一个用户对象。在该对象中有一个 DateTime 属性。当我在我的应用程序中读取它时,我收到一个错误,因为代表 DateTime 的字符串无效,它缺少 \Date ...
{System.Runtime.Serialization.SerializationException: 有一个 反序列化用户类型的对象时出错。日期时间内容 '1984-10-02T01:00:00' 不以 '/Date(' 开头并以 ')/' 结尾 根据 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;
}
}
这是我用来反序列化的方法。
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;
}
}
【问题讨论】:
标签: c# wpf web-services microsoft-metro