Click here to Skip to main content
14,767,350 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am working to deserialize a json string and write its content into a datatable. I have the following json string with jagged array and unknown dimension. Running into errors when deserialize the json into datatable.

Error: Object reference not set to an instance of an object

{
  "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
            }
    }
  ]
}


What I have tried:

I am using the below code.

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}}]}";;
            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);
            }
}
Posted
Updated 1-Dec-20 15:45pm
v4

1 solution

Look at your code:
string jsonstring;
var root = JsonSerializer.Deserialize<MyRootObject>(jsonstring);

What you pass to be deserialized contians nothing - it is null becase you have not assigned a string to the variable.

You can't deserialize nothing, that's just silly! :laugh:

Think of it this way: You have a parking space outside your house where you always park your car. If you get up one morning and leave for work but the space is empty, how are you going to drive to work?
You can't: the space contains no car.
The space is a variable: it is capable of holding a car but when it doesn't, it contains null and every time you try to use a car in an empty space you have real problems.

You need to work out where the JSON data comes from, where it is stored, and pass that to the Deserialize method - or you can't have it generate anything for you tom play with later!
   
Comments
Member 12586110 1-Dec-20 14:26pm
   
Hi OriginalGriff, Thank you for your response. I am passing the json text to the to the variable jsonstring. I updated my original post accordingly.
Richard Deeming 2-Dec-20 4:36am
   
This is C# - the variable wouldn't be null, because the code wouldn't compile. :)
CS0165 Use of unassigned local variable 'jsonstring'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900