pbj
pbj

Reputation: 719

JSON deserialize throwing exception

I am using C# code to deserialize json and write to a datatable. I run into error when json has null.

JSON

{
  "events": [
    {
      "Id": 456895,
      "Name": "Chelsea - Arsenal",
      "BetOffers": {
          "BetType": "Game",
          "Pick": "1",
          "Odds": 1.15
            }
    },
    {
      "Id": 456879,
      "Name": "Liverpool - Manchester United",
      "BetOffers": {
          "BetType": null,
          "Pick": "1",
          "Odds": null
            }
    },
    {
      "Id": 9101112,
      "Name": "Arsenal - Manchester United",
      "BetOffers": {
          "BetType": null,
          "Pick": "1",
          "Odds": null
            }
    },
        {
      "Id": 13141516,
      "Name": "Liverpool - Chelsea",
      "BetOffers": "not accepting"
    },
    {
      "Id": 123456,
      "Name": {
                "game": "Chelsea - Liverpool",
                "gameurl": "http://game.com/api/game/adfgfm"
            },
      "BetOffers": {
          "BetType": "Game",
          "Pick": "1",
          "Odds": 1.15
            }
    }
  ]
}

C#

public class Event
{
    public int Id { get; set; }
    public string Name { get; set; }
    public BetOffer BetOffers { get; set; }
}
public class BetOffer
{
    public string BetType { get; set; }
    public string Pick { get; set; }
    public double Odds { get; set; }
}
public class MyRootObject
{
    public List<Event> events { get; set; }
}

        try
        {
            string jsonstring;
            var root = JsonSerializer.Deserialize<MyRootObject>(jsonstring);

            string connectionString = "";

            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("BetType", typeof(string)));
            dt.Columns.Add(new DataColumn("Pick", typeof(string)));
            dt.Columns.Add(new DataColumn("Odds", typeof(string)));
            DataRow dr = dt.NewRow();

            for (int i = 0; i < root.events.Count; i++)
            {
                    dr = dt.NewRow();
                    dr["ID"] = root.events[i].ID;
                    dr["Name"] = root.events[i].Name;                      
                    dr["BetType"] = root.events[i].BetOffers.BetType;
                    dr["Pick"] = root.events[i].BetOffers.Pick;
                    dr["Odds"] = root.events[i].BetOffers.Odds;
                    dt.Rows.Add(dr);
            }
}

Error: Object reference not set to an instance of an object. I am trying to look for a way to fix this issue instead having to check using an if(root.events[i].BetOffers.Odds == null) condition.

Upvotes: 0

Views: 123

Answers (2)

CongSyIT
CongSyIT

Reputation: 61

should you use "?" after the data type.

  public class BetOffers    
  {
    public string BetType { get; set; } 
    public string Pick { get; set; } 
    public double? Odds { get; set; } 
  }

If you do not understand, please refer to the following document

Upvotes: 0

Farzan Hajian
Farzan Hajian

Reputation: 2019

BetOffer.Odds is double. It cannot accept null.

Using double? can help.

public class BetOffer
{
    public string BetType { get; set; }
    public string Pick { get; set; }
    public double? Odds { get; set; }
}

Upvotes: 4

Related Questions