Myh
Myh

Reputation: 105

Parsing JSON with C# and adding results to winform combobox

I am trying to create a WinForm application that parses a json file to populate a combobox.

The JSON looks like this:

{"Id":0,"Name":"default","Option":"default","Description":"default","OtherDescription":"default"}

I've created a class called Incident having properties that will be filled with the json information:

    public class Incident
{
    public Incident(int id, string name, string option, string description, string otherDescription)
    {
        Id = id;
        Name = name;
        Option = option;
        Description = description;
        OtherDescription = otherDescription;
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Option { get; set; }
    public string Description { get; set; }
    public string OtherDescription { get; set; }
}

And a list a class IncidentsList:

    public class IncidentsList
{
    public List<Incident> Incidents = new List<Incident>();

}

For some reason I am unable to populate the combobox items with the name of the incident.

Here is the entire code, any help will be highly appreciated:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //
        var incident = new Incident(0, "default", "default", "default", "default");
        // Convert object to Json
        var strResultJson = JsonConvert.SerializeObject(incident);
        // Write values as Json file
        File.WriteAllText(@"configuration.json", strResultJson);
        // Read values from file
        var strReadJson = File.ReadAllText(@"configuration.json");  
        // Convert to Json Object
        var x = JsonConvert.DeserializeObject<IncidentsList>(strReadJson);

        foreach (var option in x.Incidents.Select(p => p.Name))
        {
            comboBox1.Items.Add(option);
        }

    }

}

public class Incident
{
    public Incident(int id, string name, string option, string description, string otherDescription)
    {
        Id = id;
        Name = name;
        Option = option;
        Description = description;
        OtherDescription = otherDescription;
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public string Option { get; set; }
    public string Description { get; set; }
    public string OtherDescription { get; set; }
}

public class IncidentsList
{
    public List<Incident> Incidents = new List<Incident>();

}

Upvotes: 0

Views: 1641

Answers (1)

Andreas
Andreas

Reputation: 992

First of all, you are serializing a single incident and trying to deserialize it into a list of incidents. Secondly - i am not 100% sure, but i think - Json needs an empty constructor in the incident to deserialize, because it sets the public properties after creating an empty object.

So add your first incident to a list and then serialize that.

public partial class Form1 : Form
{
  public Form1() 
  {
    InitializeComponent();

  }

  private void Form1_Load(object sender, EventArgs e)
  {
    //
    var incident = new Incident(0, "default", "default", "default", "default");
    var list = new List<Incident>();
    list.Add(incident);
    // Convert object to Json
    var strResultJson = JsonConvert.SerializeObject(list);
    // Write values as Json file
    File.WriteAllText(@"configuration.json", strResultJson);
    // Read values from file
    var strReadJson = File.ReadAllText(@"configuration.json");  
    // Convert to Json Object
    var x = JsonConvert.DeserializeObject<List<Incident>>(strReadJson);

    foreach (var option in x.Select(p => p.Name))
    {
        comboBox1.Items.Add(option);
    }

  }

}

public class Incident
{

  public Incident()
  {
  }
  public Incident(int id, string name, string option, string description, string otherDescription)
  {
    Id = id;
    Name = name;
    Option = option;
    Description = description;
    OtherDescription = otherDescription;
  }

  public int Id { get; set; }
  public string Name { get; set; }
  public string Option { get; set; }
  public string Description { get; set; }
  public string OtherDescription { get; set; }
}

Upvotes: 1

Related Questions