JSON для объекта или списка или [дубликат] ⇐ C#

Место общения программистов C#
Anonymous
JSON для объекта или списка или [дубликат]

Сообщение Anonymous »

Я застрял при попытке десериализации объекта JSON.
Я читаю из API как HttpWebRequest и StreamReader. Я могу подтвердить, что у меня есть данные, считанные в строку, с помощью средства чтения потока ReadToEnd.
Однако я не могу напрямую десериализовать строку или средство чтения потока. Я попробовал буквально каждый пример, который смог найти здесь, на SO, но не получил результатов. Я получаю нулевой объект без данных.
Вот моя последняя попытка.

Код: Выделить всё

             HttpWebRequest request = WebRequest.Create("https://xxxxxx.xxx.com/api/Meeting?xxxxx") as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/JSON";
WebResponse response = request.GetResponse();
JsonSerializer ser = new JsonSerializer();
Root mtg;
StreamReader sr = new StreamReader(response.GetResponseStream());
string jsonstring = sr.ReadToEnd();
var jtr = new JsonTextReader(sr);

while(jtr.Read())
{
if(jtr.TokenType == JsonToken.StartObject)
{
mtg = ser.Deserialize(jtr);
}
}
В строке jsonstring содержатся данные из API. Итак, читалка работает. Но я получаю нулевой jtr для Json TextReader(sr).
Свойства объекта JSON следующие:

Код: Выделить всё

     public class Address
{
public string Line1 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Postal { get; set; }
public string Line2 { get; set; }
}

public class Agenda
{
public int ID { get; set; }
public List Documents { get; set; }
public DateTime Finalized { get; set; }
public DateTime Generated { get; set; }
public DateTime Published { get; set; }
public DateTime MeetingDate { get; set; }
public bool Revised { get; set; }
}

public class Department
{
public bool IsMeetingGroup { get; set; }
public int ID { get; set; }
public string Name { get; set; }
}

public class Document
{
public int ID { get; set; }
public string FileType { get; set; }
public DateTime FileUpdated { get; set; }
public string FileFormat { get; set; }
public string DownloadURL { get; set; }
public int FileSize { get; set; }
}

public class Location
{
public int ID { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}

public class Meeting
{
public int ID { get; set; }
public Department Department { get; set; }
public DateTime Date { get; set; }
public Type Type { get; set; }
public Location Location { get; set; }
public string Status { get; set; }
}

public class Type
{
public int ID { get; set; }
public string Name { get; set; }
}

public class Root
{
public Meeting Meeting { get; set; }
public Agenda Agenda { get; set; }
}
Вызов собрания, очевидно, не работает, а вызов Root показывает собрание и повестку дня, но они оба пусты.
Строка, которую я получаю, выглядит следующим образом:

Код: Выделить всё

[{"Meeting":{"ID":3496,"Department":{"IsMeetingGroup":true,"ID":1104,"Name":"Senior Affairs Advisory Council"},"Date":"2024-11-18T12:00:00.0000000-07:00","Type":{"ID":1037,"Name":"Regular Meeting"},"Location":{"ID":1012,"Name":"Casa","Address":{"Line1":"2540 Karsten Ct. SE","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"}},{"Meeting":{"ID":3429,"Department":{"IsMeetingGroup":true,"ID":1088,"Name":"Board of Canvass"},"Date":"2024-11-18T10:00:00.0000000-07:00","Type":{"ID":1046,"Name":"Board of Canvass"},"Location":{"ID":1078,"Name":"BC@AS Yucca Conference Room","Address":{"Line1":"415 Silver Ave.  SW","Line2":"8th Floor","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"}},{"Meeting":{"ID":3679,"Department":{"IsMeetingGroup":true,"ID":1047,"Name":"County Development Review Authority"},"Date":"2024-11-14T09:00:00.0000000-07:00","Type":{"ID":1004,"Name":"Regular Meeting"},"Location":{"ID":1069,"Name":"Zoom Video Conference","Address":{"Line1":"Zoom Video Conference","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"},"Agenda":{"ID":3326,"Documents":[{"ID":3326,"FileType":"Agenda","FileUpdated":"2024-11-05T13:46:26.5700000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=1&ID=3326","FileSize":0},{"ID":3326,"FileType":"PublicAgenda","FileUpdated":"2024-11-05T13:46:26.5700000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=14&ID=3326","FileSize":0}],"Finalized":"2024-11-05T13:40:29.0500000-07:00","Generated":"2024-11-05T13:39:31.1430000-07:00","Published":"2024-11-05T00:00:00.0000000-07:00","MeetingDate":"2024-11-14T09:00:00.0000000-07:00","Revised":false}},{"Meeting":{"ID":3559,"Department":{"IsMeetingGroup":true,"ID":1089,"Name":"Air Quality Control Board"},"Date":"2024-11-13T16:30:00.0000000-07:00","Type":{"ID":1022,"Name":"Regular Meeting"},"Location":{"ID":1069,"Name":"Zoom Video Conference","Address":{"Line1":"Zoom Video Conference","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"}},{"Meeting":{"ID":3671,"Department":{"IsMeetingGroup":true,"ID":1055,"Name":"Zoning Administrator"},"Date":"2024-11-13T09:00:00.0000000-07:00","Type":{"ID":1008,"Name":"Regular Meeting"},"Location":{"ID":1070,"Name":"Room X","Address":{"Line1":"SomePlace @ Alvarado Square","Line2":"415 Silver Ave. SW","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"},"Agenda":{"ID":3319,"Documents":[{"ID":3319,"FileType":"Agenda","FileUpdated":"2024-10-25T12:25:06.0500000-06:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=1&ID=3319","FileSize":0},{"ID":3319,"FileType":"PublicAgenda","FileUpdated":"2024-10-25T12:25:06.0500000-06:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=14&ID=3319","FileSize":0}],"Finalized":"2024-10-25T12:25:53.9070000-06:00","Generated":"2024-10-25T12:18:48.9030000-06:00","Published":"2024-10-25T00:00:00.0000000-06:00","MeetingDate":"2024-11-13T09:00:00.0000000-07:00","Revised":false}},{"Meeting":{"ID":3409,"Department":{"IsMeetingGroup":true,"ID":1000,"Name":"Board of County Commissioners"},"Date":"2024-11-12T17:00:00.0000000-07:00","Type":{"ID":1000,"Name":"Administrative Meeting"},"Location":{"ID":1070,"Name":"Room X","Address":{"Line1":"SomePlace @ Alvarado Square","Line2":"415 Silver Ave.  SW","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"}},{"Meeting":{"ID":3424,"Department":{"IsMeetingGroup":true,"ID":1000,"Name":"Board of County Commissioners"},"Date":"2024-11-12T16:00:00.0000000-07:00","Type":{"ID":1002,"Name":"Board of Finance"},"Location":{"ID":1070,"Name":"Room X","Address":{"Line1":"SomePlace @ Alvarado Square","Line2":"415 Silver Ave. SW","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"}},{"Meeting":{"ID":3420,"Department":{"IsMeetingGroup":true,"ID":1000,"Name":"Board of County Commissioners"},"Date":"2024-11-12T15:00:00.0000000-07:00","Type":{"ID":1001,"Name":"Zoning Meeting"},"Location":{"ID":1070,"Name":"Room X","Address":{"Line1":"SomePlace @ Alvarado Square","Line2":"415 Silver Ave. SW","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"}},{"Meeting":{"ID":3676,"Department":{"IsMeetingGroup":true,"ID":1051,"Name":"Local Government Coordinating Commission"},"Date":"2024-11-08T17:00:00.0000000-07:00","Type":{"ID":1103,"Name":"Sub-Committee Meeting"},"Location":{"ID":1041,"Name":"City Council Committee Room, 9th Fl.  R m   9 0 8 1 & q u o t ; , & q u o t ; A d d r e s s & q u o t ; : { & q u o t ; L i n e 1 & q u o t ; : & q u o t ; O n e   C i v i c   P l a z a   N W & q u o t ; , & q u o t ; C i t y & q u o t ; : & q u o t ; A l b u q u e r q u e & q u o t ; , & q u o t ; S t a t e & q u o t ; : & q u o t ; N M & q u o t ; , & q u o t ; P o s t a l & q u o t ; : & q u o t ; 8 7 1 0 2 & q u o t ; } } , & q u o t ; S t a t u s & q u o t ; : & q u o t ; S c h e d u l e d & q u o t ; } , & q u o t ; A g e n d a & q u o t ; : { & q u o t ; I D & q u o t ; : 3 3 2 7 , & q u o t ; D o c u m e n t s & q u o t ; : [ { & q u o t ; I D & q u o t ; : 3 3 2 7 , & q u o t ; F i l e T y p e & q u o t ; : & q u o t ; A g e n d a & q u o t ; , & q u o t ; F i l e U p d a t e d & q u o t ; : & q u o t ; 2 0 2 4 - 1 1 - 0 5 T 1 5 : 3 5 : 5 4 . 2 6 7 0 0 0 0 - 0 7 : 0 0 & q u o t ; , & q u o t ; F i l e F o r m a t & q u o t ; : & q u o t ; p d f & q u o t ; , & q u o t ; D o w n l o a d U R L & q u o t ; : & q u o t ; h t t p s : / / x x x x x x . c o m / C i t i z e n s / a c t i o n . a s p x ? T y p e = 1 & a m p ; I D = 3 3 2 7 & q u o t ; , & q u o t ; F i l e S i z e & q u o t ; : 0 } , { & q u o t ; I D & q u o t ; : 3 3 2 7 , & q u o t ; F i l e T y p e & q u o t ; : & q u o t ; P u b l i c A g e n d a & q u o t ; , & q u o t ; F i l e U p d a t e d & q u o t ; : & q u o t ; 2 0 2 4 - 1 1 - 0 5 T 1 5 : 3 5 : 5 4 . 2 6 7 0 0 0 0 - 0 7 : 0 0 & q u o t ; , & q u o t ; F i l e F o r m a t & q u o t ; : & q u o t ; p d f & q u o t ; , & q u o t ; D o w n l o a d U R L & q u o t ; : & q u o t ; h t t p s : / / x x x x x x . c o m / C i t i z e n s / a c t i o n . a s p x ? T y p e = 1 4 & a m p ; I D = 3 3 2 7 & q u o t ; , & q u o t ; F i l e S i z e & q u o t ; : 0 } ] , & q u o t ; F i n a l i z e d & q u o t ; : & q u o t ; 2 0 2 4 - 1 1 - 0 5 T 1 5 : 3 5 : 4 9 . 7 0 7 0 0 0 0 - 0 7 : 0 0 & q u o t ; , & q u o t ; G e n e r a t e d & q u o t ; : & q u o t ; 2 0 2 4 - 1 1 - 0 5 T 1 5 : 2 8 : 5 3 . 2 8 7 0 0 0 0 - 0 7 : 0 0 & q u o t ; , & q u o t ; P u b l i s h e d & q u o t ; : & q u o t ; 2 0 2 4 - 1 1 - 0 5 T 0 0 : 0 0 : 0 0 . 0 0 0 0 0 0 0 - 0 7 : 0 0 & q u o t ; , & q u o t ; M e e t i n g D a t e & q u o t ; : & q u o t ; 2 0 2 4 - 1 1 - 0 8 T 1 7 : 0 0 : 0 0 . 0 0 0 0 0 0 0 - 0 7:00","Revised":false}},{"Meeting":{"ID":3675,"Department":{"IsMeetingGroup":true,"ID":1129,"Name":"Sheriffs Office Advisory and Review Board"},"Date":"2024-11-08T13:00:00.0000000-07:00","Type":{"ID":1075,"Name":"Regular Meeting"},"Location":{"ID":1068,"Name":"Teams Video Conference Meeting","Address":{"Line1":"Teams Video Conference Meeting","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"},"Agenda":{"ID":3324,"Documents":[{"ID":3324,"FileType":"Agenda","FileUpdated":"2024-11-01T10:01:55.4970000-06:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=1&ID=3324","FileSize":0},{"ID":3324,"FileType":"PublicAgenda","FileUpdated":"2024-11-01T10:01:55.4970000-06:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=14&ID=3324","FileSize":0}],"Finalized":"2024-11-01T10:01:50.6130000-06:00","Generated":"2024-11-01T10:01:50.2900000-06:00","Published":"2024-11-01T00:00:00.0000000-06:00","MeetingDate":"2024-11-08T13:00:00.0000000-07:00","Revised":false}},{"Meeting":{"ID":3674,"Department":{"IsMeetingGroup":true,"ID":1138,"Name":"Metropolitan Detention Center Healthcare Authority"},"Date":"2024-11-08T08:30:00.0000000-07:00","Type":{"ID":1097,"Name":"Special Meeting"},"Location":{"ID":1071,"Name":"Zoom Webinar Meeting","Address":{"Line1":"Zoom Webinar Meeting","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"},"Agenda":{"ID":3323,"Documents":[{"ID":3323,"FileType":"Agenda","FileUpdated":"2024-11-04T12:30:50.0370000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=1&ID=3323","FileSize":0},{"ID":3323,"FileType":"PublicAgenda","FileUpdated":"2024-11-04T12:30:50.0370000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=14&ID=3323","FileSize":0}],"Finalized":"2024-10-29T15:16:53.8670000-06:00","Generated":"2024-11-04T12:29:05.8130000-07:00","Published":"2024-10-29T00:00:00.0000000-06:00","MeetingDate":"2024-11-08T08:30:00.0000000-07:00","Revised":true}},{"Meeting":{"ID":3677,"Department":{"IsMeetingGroup":true,"ID":1094,"Name":"Code of Conduct Review Board"},"Date":"2024-11-07T09:00:00.0000000-07:00","Type":{"ID":1027,"Name":"Regular Meeting"},"Location":{"ID":1068,"Name":"Teams Video Conference Meeting","Address":{"Line1":"Teams Video Conference Meeting","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"},"Agenda":{"ID":3325,"Documents":[{"ID":3325,"FileType":"Agenda","FileUpdated":"2024-11-04T08:57:42.6530000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=1&ID=3325","FileSize":0},{"ID":3325,"FileType":"PublicAgenda","FileUpdated":"2024-11-04T08:57:42.6530000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=14&ID=3325","FileSize":0}],"Finalized":"2024-11-04T08:57:39.2330000-07:00","Generated":"2024-11-04T08:50:38.4500000-07:00","Published":"2024-11-04T00:00:00.0000000-07:00","MeetingDate":"2024-11-07T09:00:00.0000000-07:00","Revised":false}}]
Я использую NewtwonSoft.Json и сталкивался с различными утилитами для преобразования. Text.Json и т. д.
Итак, где я это нарушил?
Я создал свои свойства на https://json2csharp.com/< /p>
Опять же, я впервые пытаюсь десериализовать JSON с помощью встроенных инструментов, поскольку раньше я просто читал файл, а затем использовал кувалду для декодирования JSON. лол
Спасибо!

Подробнее здесь: https://stackoverflow.com/questions/791 ... or-list-or

Вернуться в «C#»