-2

I'm currently working on an assignment where I need to update a list with data from my json file. However my List keeps coming up as null and I can't seem to find why any help would be greatly appreciated.

I have been trying to look around, but many of the issues I have looked at just mention on how to pull the data not how to resolve this issue.

My classes and code:

public class input
{
    DateTime signed;
    DateTime portal;

    public DateTime Signed { get => signed; set => signed = value; }
    public DateTime Portal { get => portal; set => portal = value; }
}

public class InputCollection
{
    private List<input> inputs;

    public List<input> Inputs { get => inputs; set => inputs = value; }
}

using (StreamReader streamReader = new StreamReader("C:\\Users\\Dominik\\Documents\\SenateCodingExercise\\CodingAssignment\\CodingAssignment\\input.json"))
{
    //Reads all the data in the file
    string json = streamReader.ReadToEnd();
    //converting json string to a serious of objects
    InputCollection inputCollection = JsonConvert.DeserializeObject<InputCollection>(json);
    Console.WriteLine(inputCollection.Inputs.Count);
}

My JSON file looks something like this:

{
  "Schmidt, Wayne": {
    "signed": "Friday, June 14, 2019 @ 10:58:21 PM"
  },
  "Hertel, Curtis": {
    "portal": "Wednesday, June 5, 2019 @ 10:30:36 AM"
  },
  "Daley, Kevin": {
    "signed": "Tuesday, June 4, 2019 @ 4:07:17 PM"
  }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • 2
    What you *probably* want is to eliminate `InputCollection` entirely and deserialize to a `Dictionary` as shown in as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182). But we can't say for sure without a complete, well-formed sample of your JSON. – dbc Jul 11 '19 at 01:54
  • OK, deserializing to `Dictionary` has a problem, namely that the dates are in the wrong format: https://dotnetfiddle.net/R70kmK. If I change them to strings then everything works: https://dotnetfiddle.net/26Vvzm. Do you need to parse then as dates? – dbc Jul 11 '19 at 02:00
  • Actually yes it did thank you! Yeah I'll have to parse them as dates, but that I can do. – Dominik Willaford Jul 11 '19 at 02:09

1 Answers1

0

You can deserialize your JSON to a Dictionary<string, Input>, as shown in Deserialize a Dictionary and this answer to How can I parse a JSON string that would cause illegal C# identifiers?, however you will need to make a few modifications to account for your DateTime format.

Firstly, modify your Input model as follows:

public class Input
{
    public DateTime? Signed { get; set; }
    public DateTime? Portal { get; set; }
}

Then, deserialize and re-serialize using the settings below:

var settings = new JsonSerializerSettings
{
    // Account for the custom DateTime format.
    DateFormatString = "dddd, MMMM d, yyyy @ h:mm:ss tt",
    // Do not re-serialize null `DateTime?` properties.
    NullValueHandling = NullValueHandling.Ignore,
    // Convert named c# properties -- but not dictionary keys -- to camel case
    ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() },
};

var inputs = JsonConvert.DeserializeObject<Dictionary<string, Input>>(json, settings);

var outputJson = JsonConvert.SerializeObject(inputs, Formatting.Indented, settings);

Notes:

  • Since some of the JSON entries do not have both Signed and Portal values, making the c# properties be nullable allows you to track whether or not a value was actually present in the JSON.

  • I modified your class name Input to match c# naming conventions.

  • Json.NET supports custom DateTime formats via the JsonSerializerSettings.DateFormatString setting. Documentation for the various format options can be found in Custom date and time format strings.

Demo fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340