eu quero ler o "datum" de data desta string json em uma lista de DateTime:
{
"Neujahrstag": {
"datum": "2016-01-01",
"hinweis": ""
},
"Karfreitag": {
"datum": "2016-03-25",
"hinweis": ""
},
"Ostermontag": {
"datum": "2016-03-28",
"hinweis": ""
},
"Tag der Arbeit": {
"datum": "2016-05-01",
"hinweis": ""
},
"Christi Himmelfahrt": {
"datum": "2016-05-05",
"hinweis": ""
},
"Pfingstmontag": {
"datum": "2016-05-16",
"hinweis": ""
},
"Tag der Deutschen Einheit": {
"datum": "2016-10-03",
"hinweis": ""
},
"Reformationstag": {
"datum": "2016-10-31",
"hinweis": ""
},
"1. Weihnachtstag": {
"datum": "2016-12-25",
"hinweis": ""
},
"2. Weihnachtstag": {
"datum": "2016-12-26",
"hinweis": ""
}
}
Não tenho ideia de como fazer isso.
Eu uso o seguinte código para obter a string json e analisá-la, mas não tenho ideia de como obter uma lista ou matriz.
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("https://feiertage-api.de/api/?jahr=2020&nur_land=MV");
JObject result = JObject.Parse(json);
MessageBox.Show(DateTime.ParseExact(result.First.First.First.First.ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture).ToString());
}
Existe uma maneira fácil de fazer isso ou eu tenho que escrever um desserializador de fantasias para esse tipo de json?
Uma maneira fácil seria criar uma classe para seus objetos internos:
public class Data
{
public DateTime Datum { get; set; }
public string HinWeis { get; set; }
}
Em seguida, use Json.NET para desserializar seu JSON em um Dictionary<string, Data>
e buscar cada um Datum
dos valores:
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("https://feiertage-api.de/api/?jahr=2020&nur_land=MV");
var result = JsonConvert.DeserializeObject<Dictionary<string, Data>>(json);
foreach (var kvp in result)
{
Console.WriteLine(kvp.Value.Datum.ToString("yyyy-MM-dd"));
}
}
Resultado:
2020-01-01
2020-04-10
2020-04-13
2020-05-01
2020-05-21
2020-06-01
2020-10-03
2020-10-31
2020-12-25
2020-12-26
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras