我在尝试反序列化 JSON 对象时遇到困难。
我正在从 API 中读取内容作为 HttpWebRequest 和 StreamReader。我能够确认我已使用流读取器 ReadToEnd 将数据读入字符串。
但是,我似乎无法直接反序列化字符串或流读取器。我已经尝试了几乎所有我能在这里找到的例子,但我没有得到结果。我得到一个没有数据的空对象。
这是我最新的尝试。
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<Root>(jtr);
}
}
jsonstring 包含来自 API 的数据。所以,读者正在工作。但我在 Json TextReader(sr) 上得到了 null jtr。
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<Document> 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; }
}
调用Meeting显然不起作用,调用Root显示Meeting和Agenda但它们都是空的。
我得到的字符串看起来像这样:
[{"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. Rm 9081","Address":{"Line1":"One Civic Plaza NW","City":"Albuquerque","State":"NM","Postal":"87102"}},"Status":"Scheduled"},"Agenda":{"ID":3327,"Documents":[{"ID":3327,"FileType":"Agenda","FileUpdated":"2024-11-05T15:35:54.2670000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=1&ID=3327","FileSize":0},{"ID":3327,"FileType":"PublicAgenda","FileUpdated":"2024-11-05T15:35:54.2670000-07:00","FileFormat":"pdf","DownloadURL":"https://xxxxxx.com/Citizens/action.aspx?Type=14&ID=3327","FileSize":0}],"Finalized":"2024-11-05T15:35:49.7070000-07:00","Generated":"2024-11-05T15:28:53.2870000-07:00","Published":"2024-11-05T00:00:00.0000000-07:00","MeetingDate":"2024-11-08T17:00:00.0000000-07: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 等...
那么我在哪里打破了这个?
构建了我的属性这又是我第一次尝试使用内置工具反序列化 JSON,就像过去一样,我只是读取文件,然后使用大锤解码 JSON。哈哈
谢谢!
确保您的 JSON 结构与 C# 类定义匹配。如果您的 JSON 以数组开头,请将其反序列化为
List<Root>
或类似的集合以与您的数据结构保持一致。