相关疑难解决方法(0)

反序列化Json对象 - DateTime

web-api返回一个用户对象.在那个对象中有一个DateTime属性.当我在我的应用程序中读取它时,我得到一个错误,因为代表DateTime的字符串无效,它丢失了\Date ...

{System.Runtime.Serialization.SerializationException:反序列化User类型的对象时出错.DateTime内容'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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我用来反序列化的方法.

public static T Deserialize<T>(string json) {
        try
        {
            var _Bytes = Encoding.Unicode.GetBytes(json);
            using (MemoryStream _Stream = new MemoryStream(_Bytes))
            {

                var …
Run Code Online (Sandbox Code Playgroud)

c# wpf web-services microsoft-metro

5
推荐指数
3
解决办法
4万
查看次数

标签 统计

c# ×1

microsoft-metro ×1

web-services ×1

wpf ×1