解析JSON时“对象引用未设置为对象的实例”

时间:2019-04-05 18:00:42

标签: c# json script-task ssis-2016

我是解析JSON的新手,我正在尝试使用C#在SSIS 2016中的脚本任务中解析JSON文件。我已将JSON文件复制并粘贴到VS217中,以获取必要的类:

JSON文件

{
    "success":  true,
    "timestamp":  1554234137217,
    "code":  null,
    "message":  null,
    "response":  {
                "startAt":  1501,
                "maxResults":  250,
                "total":  8959,
                 "data":  [
                              {
                                  "code":  "46346",
                                  "description":  "This is my item name",
                                  "alternateDescription":  "This might be in a a different language (could also be null value)",
                                  "quantityAvailable":  11.0,
                                  "weightAvailable":  null,
                                  "quantityOnCustomerOrder":  0.0,
                                  "weightOnCustomerOrder":  null,
                                  "quantityOnPurchaseOrder":  0.0,
                                  "weightOnPurchaseOrder":  null,
                                  "brand":  {
                                                "code":  "3388",
                                                "description":  "Vendor brand name here",
                                                "alternateDescription":  "vendor brand name in a different language (could also be null value)"
                                            },
                                  "vendor":  {
                                                 "code":  8106,
                                                 "name":  "Vendor name here",
                                                 "alternateName":  "vendor brand name in a different language (could also be null value)",
                                                 "province":  "AB",
                                                 "productCode":  "D360CCN",
                                                 "purchaseFormatCode":  "CS",
                                                 "purchaseFormatPackaging":  "1 X 8 L",
                                                 "upcCode":  "00793569000573"
                                             },
                                  "equivalentProduct":  {
                                                            "code":  "782388",
                                                            "description":  null,
                                                            "alternateDescription":  null,
                                                            "format":  {
                                                                           "code":  "CS",
                                                                           "description":  "1 X 8 L",
                                                                           "conversionFactor":  1.0
                                                                       }
                                                        },
                                  "grid":  {
                                               "code":  "200",
                                               "description":  "Regional warehouse name (could also be null value)",
                                               "alternateDescription":  "Regional warehouse alternate name (could also be null value)",
                                               "province":  "ON"
                                           },
                                  "warehouse":  {
                                                    "code":  "80",
                                                    "name":  "Name of shipping warehouse (could also be null value)"
                                                }
                              }
                          ]
             }

}

VS的课程

namespace Inventory{
public class Rootobject{
    public bool success { get; set; }
    public long timestamp { get; set; }
    public object code { get; set; }
    public object message { get; set; }
    public Response response { get; set; }
}

public class Response{
    public int startAt { get; set; }
    public int maxResults { get; set; }
    public int total { get; set; }
    public Datum[] data { get; set; }
}

public class Datum{
    public string code { get; set; }
    public string description { get; set; }
    public string alternateDescription { get; set; }
    public float quantityAvailable { get; set; }
    public object weightAvailable { get; set; }
    public float quantityOnCustomerOrder { get; set; }
    public object weightOnCustomerOrder { get; set; }
    public float quantityOnPurchaseOrder { get; set; }
    public object weightOnPurchaseOrder { get; set; }
    public Brand brand { get; set; }
    public Vendor vendor { get; set; }
    public Equivalentproduct equivalentProduct { get; set; }
    public Grid grid { get; set; }
    public Warehouse warehouse { get; set; }
}

public class Brand{
    public string code { get; set; }
    public string description { get; set; }
    public string alternateDescription { get; set; }
}

public class Vendor{
    public int code { get; set; }
    public string name { get; set; }
    public string alternateName { get; set; }
    public string province { get; set; }
    public string productCode { get; set; }
    public string purchaseFormatCode { get; set; }
    public string purchaseFormatPackaging { get; set; }
    public string upcCode { get; set; }
}

public class Equivalentproduct{
    public string code { get; set; }
    public object description { get; set; }
    public object alternateDescription { get; set; }
    public Format format { get; set; }
}

public class Format{
    public string code { get; set; }
    public string description { get; set; }
    public float conversionFactor { get; set; }
}

public class Grid{
    public string code { get; set; }
    public string description { get; set; }
    public string alternateDescription { get; set; }
    public string province { get; set; }
}

public class Warehouse{
    public string code { get; set; }
    public string name { get; set; }
}
}

脚本任务中的main.cs代码如下:

using System;
using System.Web.Script.Serialization;
using System.IO;
using Inventory;

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent{
    public override void PreExecute(){
        base.PreExecute();
    }

public override void PostExecute()
{
    base.PostExecute();
}

public override void CreateNewOutputRows()
{
    String jsonFileContent = File.ReadAllText(Variables.varInventoryFilePath);
    JavaScriptSerializer js = new JavaScriptSerializer();
    InventoryFile inventory = js.Deserialize<InventoryFile>(jsonFileContent);

    foreach (Datum item in inventory.response.data)        {
        Output0Buffer.AddRow();

        Output0Buffer.InventoryCode = item.code.ToString();
        Output0Buffer.InventoryDescription = item.description.ToString();
        Output0Buffer.InventoryAlternateDescription = item.description.ToString();
        Output0Buffer.InventoryQuantityAvailable = item.quantityAvailable.ToString();
        Output0Buffer.InventoryWeightAvailable = item.weightAvailable.ToString();
        Output0Buffer.InventoryQuantityOnCustomerOrder = item.quantityOnCustomerOrder.ToString();
        Output0Buffer.InventoryWeightOnCustomerOrder = item.weightOnCustomerOrder.ToString();
        Output0Buffer.InventoryQuantityOnPurchaseOrder = item.quantityOnPurchaseOrder.ToString();
        Output0Buffer.InventoryWeightOnPurchaseOrder = item.weightOnPurchaseOrder.ToString();

        //Output0Buffer.InventoryBrandCode = item.brand.code.ToString();
        //Output0Buffer.InventoryBrandDescription = item.brand.description.ToString();
        //Output0Buffer.InventoryBrandAlternateDescription = item.brand.alternateDescription.ToString();

        //Output0Buffer.InventoryVendorCode = item.vendor.code.ToString();
        //Output0Buffer.InventoryVendorName = item.vendor.name.ToString();
        //Output0Buffer.InventoryVendorAlternateName = item.vendor.alternateName.ToString();
        //Output0Buffer.InventoryVendorProvince = item.vendor.province.ToString();
        //Output0Buffer.InventoryVendorProductCode = item.vendor.productCode.ToString();
        //Output0Buffer.InventoryVendorPurchaseFormatCode = item.vendor.purchaseFormatCode.ToString();
        //Output0Buffer.InventoryVendorPurchaseFormatPackaging = item.vendor.purchaseFormatPackaging.ToString();
        //Output0Buffer.InventoryVendorUPCCode = item.vendor.upcCode.ToString();

        //Output0Buffer.InventoryEquivalentProductCode = item.equivalentProduct.code.ToString();
        //Output0Buffer.InventoryEquivalentProductDescription = item.equivalentProduct.description.ToString();
        //Output0Buffer.InventoryEquivalentProductAlternateDescription = item.equivalentProduct.alternateDescription.ToString();

        Output0Buffer.InventoryEquivalentProductFormatCode = item.equivalentProduct.format.code.ToString();
        Output0Buffer.InventoryEquivalentProductFormatDescription = item.equivalentProduct.format.description.ToString();
        Output0Buffer.InventoryEquivalentProductFormatConversionFactor = item.equivalentProduct.format.conversionFactor.ToString();

        Output0Buffer.InventoryGridCode = item.grid.code.ToString();
        Output0Buffer.InventoryGridDescription = item.grid.description.ToString();
        Output0Buffer.InventoryGridAlternateDescription = item.grid.alternateDescription.ToString();
        Output0Buffer.InventoryGridProvince = item.grid.province.ToString();

        Output0Buffer.InventoryWarehouseCode = item.warehouse.code.ToString();
        Output0Buffer.InventoryWarehouseName = item.warehouse.name.ToString();
    }
}

}

SSIS程序包使用上面的代码运行文件(保留上面显示的注释行),但是当我取消注释其他行之一并运行程序包时,出现以下错误消息:

Script Component has encountered an exception in user code:
Project name: SC_bd76c219e4854aa3802328ef721c2006
Object reference is not set to an instance of an object
    at ScriptMain.CreateNewOutputRows()
    at UserComponent.PrimeOutput(Int32 Outputs, Int32[] OutputIDs, PipelineBuffer[] Buffers, OutputNameMap OutputMap)
    at Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.PrimeOutput(Int32 outputs, Int32[] outputIDs, PipelineBuffer[] buffers)

我的第一个想法是对象字段不正确(即:公共对象代码{get; set;}应该读取字符串),所以我更改了声明(即:公共字符串代码{get; set;})并运行再次打包,但出现相同的错误。

我的应用程序开发机器上有完全相同的代码,并且读取完全相同的文件没有问题。我无所适从地解释了SSIS中的此错误,因为我在网上找到的每个示例都表明我的代码应该可以正常工作。

0 个答案:

没有答案