I am getting "Object reference not set to an instance of an object" error while deserialize the JSON Array Data.
Below is code used in script task in SSIS.
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Web.Script.Serialization;
using System.Collections.Generic;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
private long dataLength;
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
}
public override void CreateNewOutputRows()
{
//Get SSIS Variables
string apiUserName = Variables.APIUsername;
string apiPassword = Variables.APIPassword;
//int campaignId = (int)Variables.Campaign;
//Set Webservice URL
string wUrl = "My URL Here";
string base64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(apiUserName + ":" + apiPassword));
try
{
//Call getWebServiceResult to return our WorkGroupMetric array
Root outPutMetrics = GetWebServiceResult(wUrl, base64);
//For each group of metrics output records
//these are the column names defined in output columns in object
foreach (var metric in outPutMetrics.Rows)
{
Output0Buffer.AddRow();
Output0Buffer.EmployeeReport = metric.EmployeeReport;
Output0Buffer.EmployeeDisplayName = metric.Employee_DisplayName;
Output0Buffer.PRPayRunResultPermanentEmployeeNumber = metric.PRPayRunResultPermanent_EmployeeNumber;
Output0Buffer.OrgUnitShortName = metric.OrgUnit_ShortName;
}
}
catch (Exception e)
{
//FailComponent(e.ToString());
if (e.Message != null)
{
string ExceptionMessage = e.Message;
}
}
}
/// <returns>An array of WorkGroupMetric composed of the de-serialized JSON</returns>
private Root GetWebServiceResult(string wUrl, string base64)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
httpWReq.Headers.Add("Authorization", "Basic " + base64);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
Root jsonResponse = null;
try
{
//Test the connection
if (httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
dataLength = httpWResp.ContentLength;
string jsonString = null;
//Set jsonString using a stream reader
using (StreamReader reader = new StreamReader(responseStream))
{
jsonString = reader.ReadToEnd().Replace("\\", "");
reader.Close();
}
//Deserialize our JSON
JavaScriptSerializer sr = new JavaScriptSerializer();
sr.MaxJsonLength=2147483644;
jsonResponse = sr.Deserialize<Root>(jsonString);
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
//Output JSON parsing error
catch (Exception ex)
{
if (ex.Message != null)
{
string ExceptionMessage = ex.Message;
}
}
return jsonResponse;
}
/// <summary>
/// Outputs error message
/// </summary>
/// <param name="errorMsg">Full error text</param>
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
}
Below are C# classes from JSON2CSHARP.com
public class Row
{
public string Employee_DisplayName { get; set; }
public string PRPayRunResultPermanent_EmployeeNumber { get; set; }
public string OrgUnit_ShortName { get; set; }
public string EmployeeReport{ get; internal set; }
}
public class Data
{
public string EmployeeReport{ get; set; }
public List<Row> Rows { get; set; }
}
public class Root
{
public List<Row> Rows { get; set; }
public Data data { get; set; }
}
Could you please help me to resolved the issue as soon as possible. jsonResponse returns the data from Data and and Contains the data. But after running foreach loop it is through Object reference not set to an instance of an object error.