【发布时间】:2017-06-08 19:22:49
【问题描述】:
我正在开发一个 SSIS 包以从 API 获取一些信息。我可以在 SSIS 中使用任何组件或扩展来执行 HTTP 请求或响应操作。我的 API is in JSON format。它有两个字段 ID 和日期。我正在尝试通过提供 ID 来获取“日期”字段。
我是 C# 和 SSIS 的新手。请让我知道我是否应该尝试使用脚本组件,或者 SSIS 中是否有任何替代扩展来执行此操作。
在 SSIS 中使用脚本组件是我尝试过的。
这是我在参考this 文章后尝试过的。
public override void CreateNewOutputRows()
{
string serviceDate = Variables.TaskID;
string wUrl = "https://virtserver.swaggerhub.com/Monish/Disenrollment/1.0.0/inventory?searchString=" + serviceDate;
try
{
WorkGroupMetric[] outPutMetrics = GetWebServiceResult(wUrl);
foreach( var metric in outPutMetrics)
{
Output0Buffer.AddRow();
Output0Buffer.DisenrollmentDate = metric.CN;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
private WorkGroupMetric[] GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
WorkGroupMetric[] jsonResponse = null;
try
{
//Test the connection
if(httpWResp.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = httpWResp.GetResponseStream();
string jsonString = null;
//Set jsonString
using (StreamReader reader = new StreamReader(responseStream))
{
jsonString = reader.ReadToEnd().Replace("\\", "");
reader.Close();
}
//Desearialize our JSON
JavaScriptSerializer sr = new JavaScriptSerializer();
jsonResponse = sr.Deserialize<WorkGroupMetric[]>(jsonString.Trim('"'));
}
//Output connection error message
else
{
FailComponent(httpWResp.StatusCode.ToString());
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
return jsonResponse;
}
【问题讨论】:
标签: c# .net json web-services ssis