简体   繁体   中英

Error deserializing a JSON array in SSIS Script Component

I am attempting to deserialize a JSON array via an SSIS source script component in C# using Newtonsoft JSON.net, but I'm running into the following error when I try to build the SSIS project:

CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<ScriptMain.Order> to <ScriptMain.Order'

I suspect it's something to do with the Order class not being defined as a list, but I'm fairly new to C# and I really don't know, so would really appreciate any advice.

This was working correctly for a JSON string before I attempted to change the code to handle an array - ie in the code I changed

Order order = JsonConvert.DeserializeObject<Order>(record);

to

Order order = JsonConvert.DeserializeObject<List<Order>>(record);

Here is the JSON array - it's just a typical order / orderline scenario where one order can have multiple order lines.

[
  {
    "OrderID": 291,
    "CustomerID": 1135,
    "OrderDate": "2020-07-21",
    "OrderLine": [
      {
        "OrderLineID": 1,
        "ProductID": 2,
        "Units": 1,
        "ClientID": 2
      },
      {
        "OrderLineID": 2,
        "ProductID": 8,
        "Units": 2,
        "ClientID": 1
      }
    ]
  },
  {
    "OrderID": 292,
    "CustomerID": 59,
    "OrderDate": "2020-07-21",
    "OrderLine": [
      {
        "OrderLineID": 1,
        "ProductID": 5,
        "Units": 1,
        "ClientID": 1
      },
      {
        "OrderLineID": 2,
        "ProductID": 7,
        "Units": 2,
        "ClientID": 2
      },
      {
        "OrderLineID": 3,
        "ProductID": 9,
        "Units": 1,
        "ClientID": 3
      }
    ]
  }
]

and here is the C# from the script component in SSIS:

public override void CreateNewOutputRows()
{
    /*
      Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
      For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
    */

    string filePath = Connections.OrdersFile20200720.AcquireConnection(null).ToString();

    using (StreamReader fileContents = new StreamReader(filePath))
    {
        while (fileContents.Peek() >= 0)
        {
            string record = fileContents.ReadLine();
            //Order order = JsonConvert.DeserializeObject<Order>(record);
            Order order = JsonConvert.DeserializeObject<List<Order>>(record); //this is failing
            OrderOutputBuffer.AddRow();
            OrderOutputBuffer.OrderID = order.OrderID;
            OrderOutputBuffer.CustomerID = order.CustomerID;
            OrderOutputBuffer.OrderDate = order.OrderDate;

            foreach (OrderLine orderline in order.OrderLine)
            {
                OrderLineOutputBuffer.AddRow();
                OrderLineOutputBuffer.OrderID = order.OrderID;
                OrderLineOutputBuffer.OrderLineID = orderline.OrderLineID;
                OrderLineOutputBuffer.ProductID = orderline.ProductID;
                OrderLineOutputBuffer.Units = orderline.Units;
                OrderLineOutputBuffer.ClientID = orderline.ClientID;
            }

        }
    }
}

public class Order
{
    public int OrderID { get; set; }
    public int CustomerID { get; set; }
    public DateTime OrderDate { get; set; }
    public OrderLine[] OrderLine { get; set; }
}

public class OrderLine
{
    public int OrderLineID { get; set; }
    public int ProductID { get; set; }
    public int Units { get; set; }
    public int ClientID { get; set; }
}

}

Thanks!

I fixed this in the end:

    public override void CreateNewOutputRows()
{
    /*
      Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
      For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
    */

    string json = File.ReadAllText("Z:\\DataTech Test\\Data\\Orders_20200720.json");
    var records = JsonConvert.DeserializeObject<List<Order>>(json);

    foreach (var r in records)

    {

        OrderOutputBuffer.AddRow();
        OrderOutputBuffer.OrderID = r.OrderID;
        OrderOutputBuffer.CustomerID = r.CustomerID;
        OrderOutputBuffer.OrderDate = r.OrderDate;

        foreach (OrderLine orderline in r.OrderLine)
        {
            OrderLineOutputBuffer.AddRow();
            OrderLineOutputBuffer.OrderID = r.OrderID;
            OrderLineOutputBuffer.OrderLineID = orderline.OrderLineID;
            OrderLineOutputBuffer.ProductID = orderline.ProductID;
            OrderLineOutputBuffer.Units = orderline.Units;
            OrderLineOutputBuffer.NurseryID = orderline.NurseryID;

        }

    }


}

public class OrderLine
{
    public int OrderLineID { get; set; }
    public int ProductID { get; set; }
    public int Units { get; set; }
    public int NurseryID { get; set; }
}

public class Order
{
    public int OrderID { get; set; }
    public int CustomerID { get; set; }
    public DateTime OrderDate { get; set; }
    public List<OrderLine> OrderLine { get; set; }
}

public class Root
{
    public List<Order> Order { get; set; }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM