Deserialize DateTime from a web api

我的未来我决定 提交于 2020-01-06 06:37:06

问题


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.

Right now all the timeFrom and all the timeTo gets tha value of:

{1/1/0001 12:00:00 AM}

Witch is not the correct value...

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

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...

Thanks in avance!


回答1:


You can use Newtonsoft.Json

        var files = JObject.Parse(YourJsonhere);
        var recList = files.SelectTokens("$..timeTo").ToList();
        string value = recList[0].ToString();


来源:https://stackoverflow.com/questions/50850977/deserialize-datetime-from-a-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!