This question already has an answer here:
这个问题已经有了答案:
- Deserializing dates with dd/mm/yyyy format using Json.Net 5 answers
- 使用dd/mm/yyyy格式使用Json反序列化日期。净5答案
I have this Json from a web api:
这个Json来自web api:
jsonstring ={"users":[{"id":1123,"last_update":"2016-02-28 14:53:04"}],"page":1,"pages":1}
which I want to deserialize in an object like:
我想在对象中反序列化如下:
public class Rootobject
{
public User[] users { get; set; }
public int page { get; set; }
public int pages { get; set; }
}
public class User
{
public int id { get; set; }
public DateTime last_update { get; set; }
}
for this I use:
我使用:
var obj= JsonConvert.DeserializeObject<Rootobject>(jsonString);
the result has null
for last_update
.
对于last_update,结果为空。
jsonstring
is a string result from WebClient.DownloadString(url)
; which look like above example.
jsonstring是来自WebClient.DownloadString(url)的字符串结果;看起来像上面的例子。
How can I get the date on deserialization?
我怎样才能得到反序列化的日期?
Edit:
编辑:
None of the solutions from this post Deserializing dates with dd/mm/yyyy format using Json.Net help me fix my issue.
本文中使用dd/mm/yyyy格式使用Json反序列化日期的解决方案都没有。Net帮助我解决问题。
3 个解决方案
#1
2
var obj = JsonConvert.DeserializeObject<Rootobject>(jsonString,
new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
小提琴
#2
0
Change the property last_update
as Nullable, Then it allows you to assign the null literal to the DateTime type. It provides another level of indirection. So use like the following:
将属性last_update更改为Nullable,然后它允许您将null文字分配给DateTime类型。它提供了另一个层次的间接。所以使用如下:
public DateTime? last_update { get; set; }
This is for accepting The date even if it is null, you can specify the Date format While Deserializing the JSON. Use this Thread for that
这是为了接受日期,即使它是空的,您也可以在反序列化JSON时指定日期格式。用这个线程
#3
-1
"2016-02-28 14:53:04"
is not a valid RFC3339 date time, and I think it can't parse it because of that.
“2016-02-28 14:53:04”不是一个有效的RFC3339日期时间,我认为它无法解析它。
It has to be:
它必须是:
2016-02-28T14:53:04
Also note that the can't be null, since DateTime
is a struct. To make it nullable, make the data type DateTime?
which does allow null values.
还要注意不能为空,因为DateTime是一个结构体。要使它为nullable,请令数据类型DateTime?它允许空值。
#1
2
var obj = JsonConvert.DeserializeObject<Rootobject>(jsonString,
new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
小提琴
#2
0
Change the property last_update
as Nullable, Then it allows you to assign the null literal to the DateTime type. It provides another level of indirection. So use like the following:
将属性last_update更改为Nullable,然后它允许您将null文字分配给DateTime类型。它提供了另一个层次的间接。所以使用如下:
public DateTime? last_update { get; set; }
This is for accepting The date even if it is null, you can specify the Date format While Deserializing the JSON. Use this Thread for that
这是为了接受日期,即使它是空的,您也可以在反序列化JSON时指定日期格式。用这个线程
#3
-1
"2016-02-28 14:53:04"
is not a valid RFC3339 date time, and I think it can't parse it because of that.
“2016-02-28 14:53:04”不是一个有效的RFC3339日期时间,我认为它无法解析它。
It has to be:
它必须是:
2016-02-28T14:53:04
Also note that the can't be null, since DateTime
is a struct. To make it nullable, make the data type DateTime?
which does allow null values.
还要注意不能为空,因为DateTime是一个结构体。要使它为nullable,请令数据类型DateTime?它允许空值。