Deserialize json which has a string and complex object

Raj D 591 Reputation points
2020-12-02T03:34:39.073+00:00

Hi, I am trying to deserialize a json string and write its content into a datatable. I have the following json string with either a string or custom object and not sure how to parse between the string and custom object to write data to the datatable. Running into error NullReference Exception (Object reference not set to an instance of an object) when I deserialize the json.

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 = "{\"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}}]}";; //sample json
            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 ?? (object)DBNull.Value);;
                    dr["Pick"] = root.events[i].BetOffers.Pick;
                    dr["Odds"] = (root.events[i].BetOffers.Odds ?? (object)DBNull.Value);;
                    dt.Rows.Add(dr);
            }
}

Could you please guide me how to deserialize jagged json arrays.

Thank you

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Cheong00 3,476 Reputation points
    2020-12-03T01:41:15.493+00:00

    I got no error running your code other than the incorrect casing of ID -> Id at line 36.

    https://dotnetfiddle.net/Ix9vVv


  2. Daniel Zhang-MSFT 9,626 Reputation points
    2020-12-03T02:50:06.317+00:00

    Hi RajD-9527,
    Based on your code, there are some errors you need to note.
    In your C# code:
    You need to change dr["ID"] = root.events[i].ID to dr["ID"] = root.events[i].Id;
    In your JSON:

     "BetOffers": "not accepting"  
    

    There are three attributes in BetOffers , so you need to clearly specify the attributes and corresponding values.

     "Name": {  
                 "game": "Chelsea - Liverpool",  
                 "gameurl": "http://game.com/api/game/adfgfm"  
              },  
    

    There is no game or gameurl attribute in Name.
    After correcting these errors, I tested your code(include jsonstring and JSON file) and it worked fine.
    Test code:

    //  string jsonstring = "{\"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}}]}"; ; //sample json  
    string jsonString = File.ReadAllText(@"C:\Users\Desktop\Test.json");  
    var root = JsonSerializer.Deserialize<MyRootObject>(jsonString);  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.