从Web API反序列化DateTime [英] Deserialize DateTime from a web api
问题描述
所以我有一个sql数据库,我通过json API
从中获取值.
我对反序列化DateTime
变量timeFrom
和timeTo
有一些问题.
So I have an sql database where i get values from via a json API
.
I have som problem with deserializing the DateTime
variables timeFrom
and timeTo
.
现在所有timeFrom
和所有timeTo
的值都为:
Right now all the timeFrom
and all the timeTo
gets tha value of:
{1/1/0001 12:00:00 AM}
{1/1/0001 12:00:00 AM}
女巫的值不正确...
Witch is not the correct value...
这是通过json API预订房间的一个示例:
This is a sample of one booking of a room from the json api:
{
"id": "c49af34d-5479-4304-8acc-30f22a7cd1ef",
"code": 6679,
"timeFrom": "2018-06-13T16:00:00",
"timeTo": "2018-06-13T16:45:00",
"note": null,
"createdDate": "2018-02-13T10:04:14.8",
"room": {
"name": "Rum 1",
"id": "c49af34d-5479-4304-8acc-30f22a7cd1ef",
"seats": 10,
"availableFrom": null,
"availableTo": null,
"roomAttributes": [
{
"id": 1,
"name": "Tv",
"icon": 62060
},
{
"id": 2,
"name": "Wifi",
"icon": 61931
}
]
}
}
这就是我反序列化json的方式:
And this is how I deserialize the json:
string booking = "https://api.booknings.com/api/company/c49af34d-5479-4304-8acc-30f22a7cd1ef/room/{room.id}/booking";
HttpClient BookingClient = new HttpClient();
string BookingResponse = await BookingClient.GetStringAsync(booking);
var BookingData = JsonConvert.DeserializeObject<List<Bookings>>(response);
foreach (var books in BookingData)
{
string note = books.note;
DateTime TimeFrom = books.timeFrom;
DateTime TimeTo = books.timeTo;
Class2 BookRoom = books.room;
string BookId = books.id;
int code = books.code;
}
这是我拥有预订的所有属性的课程:
This is the class where I have all the properties of the booking:
public class Bookings
{
public string id { get; set; }
public int code { get; set; }
public DateTime timeFrom { get; set; }
public DateTime timeTo { get; set; }
public string note { get; set; }
public DateTime createdDate { get; set; }
public Class2 room { get; set; }
}
所以我的问题是,是否有人知道我在做什么错...
So my question is if there is someone who knows what i am doing wrong...
非常感谢!
推荐答案
您可以使用Newtonsoft.Json
You can use Newtonsoft.Json
var files = JObject.Parse(YourJsonhere);
var recList = files.SelectTokens("$..timeTo").ToList();
string value = recList[0].ToString();
这篇关于从Web API反序列化DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!