反序列化JSON对象 - 日期时间 [英] Deserialize Json Object - DateTime

查看:149
本文介绍了反序列化JSON对象 - 日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网​​络API 返回一个用户对象。在该对象有一个的DateTime 属性。当我'读它在我的应用程序,我得到一个错误,因为这将代表了日期时间的字符串无效它缺少 \Date ...




{System.Runtime.Serialization.SerializationException:有是一个
错误反序列化类型用户的对象。日期时间内容
'1984-10-02T01:00:00不以'/日期开始('和结束')/'$ B $按要求JSON湾--->




 公共静态异步任务<使用者>使用的getUser(字符串email)
{

{
(HttpClient的客户端=新的HttpClient())
{
HttpResponseMessage响应=等待client.GetAsync (?电子邮件=网址+ +电子邮件);
如果(response.IsSuccessStatusCode)
{
字符串内容=等待response.Content.ReadAsStringAsync();
用户的用户= DataService.Deserialize<使用者>(内容);
回报用户;
}
返回NULL;
}
}
赶上(异常前)
{
返回NULL;
}
}

这是我使用反序列化方法。

 公共静态牛逼反序列化< T>(字符串JSON){

{
变种_Bytes = Encoding.Unicode.GetBytes(JSON);
使用(MemoryStream的_Stream =新的MemoryStream(_Bytes))
{

变种_Serializer =新DataContractJsonSerializer(typeof运算(T));

回报率(T)_Serializer.ReadObject(_Stream);
}
}
赶上(异常前)
{
罚球前;
}
}


解决方案

我。发现如何将包Json.net(Newtonsoft.Json)时修复它。

 公共异步静态任务< T>反序列化< T>(JSON字符串)
{
VAR值=等待Newtonsoft.Json.JsonConvert.DeserializeObjectAsync< T>(JSON);
返回值;
}


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

解决方案

I found how to fix it when adding the package Json.net (Newtonsoft.Json).

public async static Task<T> Deserialize<T>(string json)
    {
        var value = await Newtonsoft.Json.JsonConvert.DeserializeObjectAsync<T>(json);
        return value;
    }

这篇关于反序列化JSON对象 - 日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆