1

I'm developing a program that deserializes JSON data into a list of a custom class and then uploads it all to my SQL Server.

string json = streamReader.ReadToEnd();
JObject jsonEntriesRaw = JsonConvert.DeserializeObject( json ) as JObject;
JArray jsonEntries = jsonEntriesRaw[ "events" ] as JArray; //since the data i actually need is wrapped in "events"
List<MoveUpdateEntry> entries = jsonEntries.ToObject<List<MoveUpdateEntry>>(); //error occurs here

My custom class...

public class MoveUpdateEntry
{
    public DateTime errorCreateDate { get; set; }
}

...has a (Sql?)DateTime field that I have no clue how to properly parse data into.

The issue I'm facing is that the program doesn't understand how to work with the specific date format I'm dealing with (yyyyMMdd hh:mm:ss).

Unhandled Exception: Newtonsoft.Json.JsonReaderException: Could not convert string to DateTime: 20200630 18:19:01. Path 'events[0].errorCreateDate'.

How can I make the JsonReader know what type of formatting to expect?

sirius_pain
  • 34
  • 3
  • 6
  • 21
  • what does json looks like? – Nonik Aug 06 '20 at 16:47
  • @Nonik `{"errorCreateDate":"20200619 16:19:06"}` – sirius_pain Aug 06 '20 at 16:53
  • your json, does not look like an array, also where is "events" property in your JSON – Nonik Aug 06 '20 at 17:01
  • @Nonik My mistake! I showed you only a single instance and its relevant information. The actual json itself looks more like this: `{"events":[{"errorCreateDate":"20200619 16:19:06","affectedPieceCount":null}]}`. The rest of the objects outside of `events` are irrelevant to what I am doing – sirius_pain Aug 06 '20 at 17:04
  • You need to load initially using `JsonSerializerSettings.DateParseHandling = DateParseHandling.None`. Then afterwards you can deserialize to your desired `DateTime` format. See: [JToken: Get raw/original JSON value](https://stackoverflow.com/a/35141787/3744182) and [JSON.NET: Get Specific JSON Date Value](https://stackoverflow.com/a/35166887/3744182) and [Converting JToken into .NET types with custom SerializerSettings](https://stackoverflow.com/a/54912445/3744182). In fact I think this is a duplicate of those, agree? – dbc Aug 06 '20 at 17:33
  • 1
    Or is your question, *How can I create a `JsonConverter` for dates and times that recognizes the format `yyyyMMdd hh:mm:ss`?* If so you can use `JsonSerializerSettings.DateFormatString` as shown in [Newtonsoft.Json customize serialize dates](https://stackoverflow.com/a/38276379/3744182). – dbc Aug 06 '20 at 17:51
  • @dbc That 2nd link was exactly what I needed! Thanks so much! Am I able to specify multiple formats for it to look for? – sirius_pain Aug 06 '20 at 17:57
  • 1
    Take a look at [Supporting multiple custom DateTime formats when deserializing with Json.Net](https://stackoverflow.com/q/51318713/3744182). Or you could add a `JsonConverter` directly to the property as shown in [Specifying a custom DateTime format when serializing with Json.Net](https://stackoverflow.com/q/18635599/3744182). – dbc Aug 06 '20 at 17:59
  • 1
    @dbc WOW, that answer regarding the custom class did the trick! You've helped me more than you know. Is it too early to say I love you? – sirius_pain Aug 06 '20 at 18:25

1 Answers1

0

You are missing a simple DateTime parse.

Can you try this?

List<MoveUpdateEntry> entries = jsonEntries
                                .ToObject<List<string>>() // replace string with the expected type.
                                .Select(x => new MoveUpdateEntry{
                                    errorCreateDate = DateTime.ParseExact(x, "yyyyMMdd HH:mm:ss", null)
                                })
                                .ToObject<List<MoveUpdateEntry>>();
dragos
  • 183
  • 1
  • 1
  • 13
  • I get an `IEnumerable does not contain definition...` error about there not being a definition for that last `.ToObject>();`, but `.ToList();` makes it past the compiler. With the latter, the error becomes `Error reading string. Unexpected token: StartObject. Path 'events[0]'.` – sirius_pain Aug 06 '20 at 17:42