Deserialize json using JavaScriptSerializer

Raj D 591 Reputation points
2020-12-08T23:14:52.293+00:00

Hi, I am trying to parse a json string and write it into a table using JavaScriptSerializer() in SSIS Script Task C#.

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

public class Event
{
    public int Id { get; set; }
    public Names Name { get; set; }
    public BetOffer BetOffers { get; set; }
}
public class Names
{
    public string game { get; set; }
    public string gameurl { 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; }
}

        static void Main(string[] args)
        {
            string json;
            json = getGameBets(); //API call generates json shown above
            dynamic root = JavaScriptSerializer.Deserialize<dynamic>(json) //write deserialized json into a table
        }
    }
}

I am not sure how to parse once I have read json content into the variable root. Please guide me.

Thank you.

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,577 questions
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,918 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2020-12-09T03:04:38.733+00:00

    You can use dynamic to get the result without error, but it will be slightly troublesome to process. Since you have defined several classes, it is better to use these classes instead of dynamic, just modify some small problems.

    1. The value of Name and BetOffers may be a type you define, but it may also be a string, so we need an object compatible with these two types, so the Event class is best defined as follows:
      public class Event  
      
      {
      public int Id { get; set; }
      public object Name { get; set; }
      public object BetOffers { get; set; }
      }
    2. According to the Json structure, you also need to define an additional class public class Rootobject
      {
      public Event[] events { get; set; }
      }
      Then, you can get an object of type RootObject.
      var serializer = new JavaScriptSerializer();  
      Rootobject root = serializer.Deserialize<Rootobject>(json);  
      
      If you need to use Name or BetOffer in the subsequent code, you can convert them to Dictionary<TKey,TValue>:
              foreach (var eventObject in root.Events)  
              {  
                  if (eventObject.BetOffers is Dictionary<string,object>)  
                  {  
                      Dictionary<string, object> bitOffers = (Dictionary<string, object>)eventObject.BetOffers;  
                  }  
                  if (eventObject.Name is Dictionary<string, string>)  
                  {  
                      Dictionary<string, string> name = (Dictionary<string, string>)eventObject.Name;  
                  }  
              }  
      
      If you want to use your defined class instead of Dictionary, you can also convert it like this:
          public static BetOffer MapDictionaryToBetOffer(Dictionary<string, object> dic)  
          {  
              return new BetOffer  
              {  
                  //BetType =dic["BetType"].ToString()  
                  BetType = dic["BetType"] == null ? null : dic["BetType"].ToString(),  
                  Pick = dic["Pick"].ToString(),  
                  Odds = Convert.ToDouble(dic["Odds"])  
              };  
          }  
      
      Update:
      Unfortunately, there is no such Attribute in JavaScriptSerializer, we may need to customize one by ourselves.

    Please refer to the response in this link:
    JavaScriptSerializer - custom property name

    In addition, the MapDictionaryToBetOffer method I provided above is flawed. I did not handle the situation when BetType is null. When it is null, the original method will cause NullReferenceException.

    Do you have to use JavaScriptSerializer? Is it possible to replace other packages to handle Json? For example, System.Text.Json, you can install it directly in nuget, and then use JsonPropertyNameAttribute to achieve what you need.
    If you can change the package, please tell me, the above code will need to be modified a lot.


    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.


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.