0

Im tryng to parse dates in the format 'dd-mm-yyyy', but when the first object in the json has a null value in a date field it parses the dates in the format 'mm-dd-yyyy', causing dates like 9 of 31, 2021

pDs = JsonConvert.DeserializeObject<DataSet>(json);

json sample

{"dados" : [{"cd_ordem_servico":64773,"cd_solicitante":24379,"cd_tecnico":"24638","cd_responsavel":24379,"nm_tecnico":"Giovane \"O Inocente\" BDT","cliente":null,"nm_cidade":null,"dt_inicio":null,"dt_fim":null,"tp_duracao":null,"tp_viagem":"0","cd_status":"4","cd_status_os":4,"qt_produtos":0,"qt_contatos":0,"fl_turma_criada":0,"cd_head":0,"cd_status_os_complementar":null,"cd_head_solicitante":0,"cd_gerente_solicitante":256,"fl_complementar":"0","fl_data_hora":0,"fl_item_cht":1,"cd_gerente_tecnico":256,"sou_cs":0,"cd_criador":"24379","ds_etapa":null,"nm_solicitante":"Émerson Ariel Schmitt BDT","nm_estado":null,"fl_pedido":"Sim","ds_local":null,"ds_envio_hardware":"Não","fl_envio_hardware":0,"fl_enviado":null,"dt_envio_hardware":null,"ds_enviado":"N/A","ds_status":null,"ds_marcadores":"","cd_marcadores":"","nm_transporte":null}, {"cd_ordem_servico":64760,"cd_solicitante":17438,"cd_tecnico":"17438","cd_responsavel":17438,"nm_tecnico":"Ágatha Mile de Almeida Rodrigues BDT","cliente":" CLIENTE TESTE DSV","nm_cidade":"Americana","dt_inicio":"2021-06-30T08:00:00","dt_fim":"2021-06-30T09:00:00","tp_duracao":1.0,"tp_viagem":"0","cd_status":"6","cd_status_os":6,"qt_produtos":1,"qt_contatos":1,"fl_turma_criada":0,"cd_head":0,"cd_status_os_complementar":"1","cd_head_solicitante":0,"cd_gerente_solicitante":256,"fl_complementar":"0","fl_data_hora":1,"fl_item_cht":1,"cd_gerente_tecnico":256,"sou_cs":0,"cd_criador":"17438","ds_etapa":null,"nm_solicitante":"Ágatha Mile de Almeida Rodrigues BDT","nm_estado":"SP","fl_pedido":"Não","ds_local":"SKA (Americana/SP)","ds_envio_hardware":"Não","fl_envio_hardware":0,"fl_enviado":null,"dt_envio_hardware":null,"ds_enviado":"N/A","ds_status":"Não se aplica","ds_marcadores":"","cd_marcadores":"","nm_transporte":"Avião"}]}

If you inspect the output DataSet the dates in dt_inicio are all wrong.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • @dbc i added the json, if you inspect the output DataSet the dates in dt_inicio are all wrong – Giovane Winck Oct 01 '21 at 19:21
  • I guess the "N/A" isn't a valid type to be deseriallized, try puting null instead – Hasagiii Oct 01 '21 at 19:27
  • OK, [mcve] here: https://dotnetfiddle.net/jPrj20. [DateTime column type becomes String type after deserializing DataTable](https://stackoverflow.com/q/37109154/3744182) might be related. – dbc Oct 01 '21 at 19:39
  • Do you care if the inferred data column type for `dt_inicio` is `string` or `DateTime`? 2) If `DateTime` inferencing is required, does `TypeInferringDataTableConverter` from [DateTime column type becomes String type after deserializing DataTable](https://stackoverflow.com/q/37109154/3744182) answer your question? You will also need to use `DataSetConverter` from [DateTime column type becomes String type after deserializing DataTable property on Custom Class](https://stackoverflow.com/a/32731816/3744182) to force Json.NET to use this converter. – dbc Oct 02 '21 at 03:53
  • If `DateTime` inferencing is not required, use [`JsonSerializerSettings {DateParseHandling = DateParseHandling.None}`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DateParseHandling.htm) and you should be all set, the `dt_inicio` will be of type `string` and the values will be as they are in the JSON file without reformatting. See: [Json.NET Disable the deserialization on DateTime](https://stackoverflow.com/q/11856694/3744182). – dbc Oct 02 '21 at 16:41

1 Answers1

0

You can specify the date format in the JsonSerializerSettings.

pDs = JsonConvert.DeserializeObject<DataSet>(json, 
    new JsonSerializerSettings { DateFormatString = "dd-mm-yyyy" }); 
hawkstrider
  • 4,141
  • 16
  • 27