Not_A_SysAdmin
Not_A_SysAdmin

Reputation: 136

Json.net incorrect datetime format

I started using Json.net to parse a Json string with date values. Everything went okay safe for dates. So far I've been unable to convert JValue to .NET DateTime, even using the DateTime.Parse method, because Json.NET seems to parse dates into an unusual format which looks like this

"Wed Jul 20 16:00:00 CEST 2016"

I managed to reformat and parse the dates using regular expressions, but for future use I'd like to learn to properly use Json.net rather than reinvent the wheel.

the JSON string is as follows

{
"totalCount": 5,
"avis": [{
    "idDossier": 422271,
    "typeDocDCE": 1,
    "dcevisible": 0,
    "organisme": "OPAC du Rhône",
    "idOrganisme": 5687,
    "reference": "16S0012",
    "idTypeProcedure": 68,
    "typeProcedure": "Procédure adaptée",
    "idTypeMarche": 1,
    "typeMarche": "Travaux",
    "libelle": "CHAMBOST ALLIERES - Les Cités - Construction de 16 logements individuels et intermédiaires",
    "dateRemiseOffre": "20/07/2016 00:00",
    "dateRemiseCandidature": "",
    "datePublication": "23/06/2016 17:48",
    "dateLimite": "20/07/2016 16:00",
    "idFichierRC": 0,
    "rectificatifs": true,
    "questions": true,
    "mps": false
}, {
    "idDossier": 422402,
    "typeDocDCE": 6,
    "dcevisible": 0,
    "organisme": "OPAC du Rhône",
    "idOrganisme": 5687,
    "reference": "16S0010",
    "idTypeProcedure": 2,
    "typeProcedure": "Appel d'offres restreint",
    "idTypeMarche": 2,
    "typeMarche": "Services",
    "libelle": "Maintenance des installations de chauffage et ECS avec intéressement et prestations de réparation sur le patrimoine de l’Opac du Rhône - Lot n°3 : chaufferies et installations de chauffage collectif et électriques des agences de Thizy et L’Arbresle",
    "dateRemiseOffre": "",
    "dateRemiseCandidature": "25/07/2016 00:00",
    "datePublication": "27/06/2016 11:43",
    "dateLimite": "25/07/2016 16:00",
    "idFichierRC": 0,
    "rectificatifs": false,
    "questions": false,
    "mps": false
}]

}

And here is my C# code

            string JsonString = new WebClient().DownloadString(URL);
        JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
        {
            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
            DateTimeZoneHandling = DateTimeZoneHandling.Local
        };
        dynamic elements = JsonConvert.DeserializeObject(JsonString, microsoftDateFormatSettings);
        for (int i = 0; i < int.Parse(elements.totalCount.ToString()) ; i++)
        {
            idDossier = elements.avis[i].idDossier;
            refs = elements.avis[i].
            PublicationDate = elements.avis[i].datePublication;
            Deadline = elements.avis[i].dateLimite;

What did I do wrong ?

Thanks in advance

Upvotes: 2

Views: 1924

Answers (1)

Aleksey L.
Aleksey L.

Reputation: 38046

You can specify DateFormatString in JsonSerializerSettings

var json = @"{""dateRemiseCandidature"": ""25/07/2016 00:00""}";
var settings = new JsonSerializerSettings
{
    DateFormatString = "dd/MM/yyyy hh:mm"
};
var result = JsonConvert.DeserializeObject<SomeClass>(json, settings);

//////

class SomeClass
{
    public DateTime dateRemiseCandidature { get; set; }
}

Upvotes: 1

Related Questions