在SSIS中使用脚本任务导入Json文件

2024-07-04

我是在 SSIS 中使用脚本任务导入 API 的新手。 我必须管理一个简单的 API JSON 文件的导入,但这第二个 API JSON 文件有点棘手。我一直在看代码,只是不知道我做错了什么。

我的 JSON 文件有一个标头,我需要确定需要循环访问多少页 API 才能获取数据,但我迷失的是如何导入下一位数据。我想我已经很接近了,但由于我是新手,我可以在一些指导下做到这一点。

我在下面的脚本任务中的代码,已经看到流读取器导入数据,但我不知道使用什么(例如列表、类或字典)来导入countIn, countOut等栏目。

    try
        {
            //Call getWebServiceResult to return our WorkGroupMetric array
            WorkGroupMetric[] outPutMetrics = GetWebServiceResult(wUrl);

            //For each group of metrics output records
            foreach (var metric in outPutMetrics)
            {
                APIBuffer.AddRow();
                APIBuffer.count = metric.count;
                APIBuffer.currentpage =  metric.currentpage;
                APIBuffer.totalpages = metric.totalpages;
                APIBuffer.countIn = metric.result.countIn;
                APIBuffer.countOut = metric.result.countOut;
                APIBuffer.type = metric.result.type;
                APIBuffer.countLine = metric.result.countLine;
                APIBuffer.from = metric.result.from;
                APIBuffer.to = metric.result.to;
            }

        }
        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 a stream reader
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    jsonString = reader.ReadToEnd().Replace("\\", "");
                    reader.Close();
                }

                //Deserialize our JSON
                JavaScriptSerializer sr = new JavaScriptSerializer();
                //JSON string comes in with a leading and trailing " that need to be removed for parsing to work correctly
                //The JSON here is serialized weird, normally you would not need this trim
                jsonResponse = sr.Deserialize<WorkGroupMetric[]>(jsonString.Trim('"'));

            }
            //Output connection error message
            else
            {
                FailComponent(httpWResp.StatusCode.ToString());

            }
        }

        //Output JSON parsing error
        catch (Exception e)
        {
            FailComponent(e.ToString());
        }
        return jsonResponse;

        throw new NotImplementedException();
    }

    private void FailComponent(string errorMsg)
    {
        bool fail = false;
        IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
        compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);

    }
}
//Class to hold our workgroup metrics
class WorkGroupMetric
{
    public string count { get; set; }
    public string currentpage { get; set; }
    public string totalpages  { get; set; }
    public List<Result> result { get; set; }   

}

class Result
{
    public string countIn { get; set; }
    public string countOut { get; set; }
    public string type { get; set; }
    public string countLine { get; set; }
    public string from { get; set; }
    public string to { get; set; }

}

所以我已经让代码正常工作了,将其发布以便将来可以帮助别人。

   try
        {
            //Call getWebServiceResult to return our WorkGroupMetric array
            WorkGroupMetric outPutMetrics = GetWebServiceResult(wUrl);

            //For each group of metrics output records
            //foreach (var metric in outPutMetrics)
            //{


            var ts = outPutMetrics.results;

            totalcount = Int32.Parse(outPutMetrics.count);

            foreach (var a in ts)
            {
                Output0Buffer.AddRow();
                //Output0Buffer.count = outPutMetrics.count;
                
                Output0Buffer.countIn = a.countIn;
                Output0Buffer.countOut = a.countOut;
                Output0Buffer.type = a.type;
                Output0Buffer.countLine = a.countLine;
                Output0Buffer.from = a.from;
                Output0Buffer.to = a.to;

                i = i + 1;
            }

            Console.Write(i);
        }
        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 a stream reader
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    jsonString = reader.ReadToEnd().Replace("\\", "");
                    reader.Close();
                }

                //Deserialize our JSON
                JavaScriptSerializer sr = new JavaScriptSerializer();
                //JSON string comes in with a leading and trailing " that need to be removed for parsing to work correctly
                //The JSON here is serialized weird, normally you would not need this trim
                jsonResponse = sr.Deserialize<WorkGroupMetric>(jsonString.Trim('"'));

            }
            //Output connection error message
            else
            {
                FailComponent(httpWResp.StatusCode.ToString());

            }
        }

        //Output JSON parsing error
        catch (Exception e)
        {
            FailComponent(e.ToString());
        }
        return jsonResponse;

        throw new NotImplementedException();
    }

    private void FailComponent(string errorMsg)
    {
        bool fail = false;
        IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
        compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);

    }
}
//Class to hold our work group metrics
class WorkGroupMetric
{
    public string count { get; set; }
    public string currentpage { get; set; }
    public string totalpages { get; set; }
    public Result[] results { get; set; }

}

class Result
{
    public string countIn { get; set; }
    public string countOut { get; set; }
    public string type { get; set; }
    public string countLine `enter code here`{ get; set; }
    public string from { get; set; }
    public string to { get; set; }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在SSIS中使用脚本任务导入Json文件 的相关文章

随机推荐

  • maven-failsafe-plugin 错误并构建成功?

    我的问题与此非常相似 maven failsafe plugin 失败并构建成功 https stackoverflow com questions 12279160 maven failsafe plugin failures and b
  • Android Studio:Imageview 背景在比图像更大的最小尺寸上模糊

    在 Android Studio 中 我正在使用 Imageviews 并使用相对较小的图像 10x16px 来获得较大的图像视图 100 像素 就像任何人都会做的那样 我根据需要设置图像视图的最小高度 宽度 Imageview setMi
  • SocketIO Chrome 检查器框架

    我正在使用 Socket IO 在 chrome 检查器中查看帧时遇到了一些问题 每帧内容旁边的数字是什么意思 这就是 Engine io 协议 其中您看到的数字是数据包编码
  • Hibernate 5. 生成 SQL DDL 到文件中

    我尝试使用这个类 Hibernate JPA 在更新 DB 架构之前检查生成的 sql 如 NET EF 迁移 https stackoverflow com questions 30833260 hibernate jpa check g
  • 除了Azure Portal之外,还有查看Application Insights日志的方法吗?

    我无法忍受在 Azure 门户中多次单击来访问 AppInsights 日志分析 将 URL 保存到分析刀片并返回到它也经常无法加载页面 因为似乎存在一些身份验证令牌过期问题 如何在不使用 Azure 门户的情况下在 AppInsights
  • Eslint:无重复解决错误:无法加载解析器“节点”

    我今天刚刚更新了我的项目 带有 VueJS 和 Quasar Framework 的 SPA npm update我现在无法运行它 我收到错误no duplicates Resolve error unable to load resolv
  • Android SKIA 图像解码

    HI 我目前正在研究Android如何解码和图像文件 当我检查代码时 它似乎正在调用 SKIA 库 但是 我如何根据源代码知道android skia支持哪些图像文件格式呢 我不是编程专家 所以我仍在尝试了解C 和Java语言 我现在迷失在
  • MVC_layout页面中脚本放置的位置

    您好 通过阅读本网站上的建议 我了解到脚本应位于 layout 页面的底部 我的问题是我不确定 底部 到底在哪里 有些人说它就在标签之前 但这对我不起作用 我尝试过将脚本放在很多地方 但似乎没有地方起作用 有人可以告诉我我做错了什么吗 这是
  • Decimal 存储 C# 中解析字符串的精度?有什么影响?

    在 IRC 的一次对话中 有人指出了以下几点 decimal Parse 1 0000 ToString 1 0000 decimal Parse 1 00 ToString 1 00 如何 为什么decimal类型像这样保留精度 或者更确
  • 复合主键:好还是坏?

    虽然可以使用复合主键 但是对于下面的情况 这真的是一种不好的做法吗 Stackoverflow 上的共识在这个问题上似乎是双向的 Why 我想将订单付款存储在单独的表中 原因是 一个订单可以有许多项目 这些项目以多对多关系的形式在单独的表中
  • 在 C 中打印 Unicode 符号

    我正在尝试打印 unicode 星号字符 0x2605 http www fileformat info info unicode char 2605 index htm 在使用 C 的 Linux 终端中 我遵循了网站上其他答案建议的语法
  • 如何获取谷歌地图 v2 api 密钥以进行团队合作

    抱歉我的英语不好 我的 google 地图 api v2 密钥有问题 我和我的团队一起工作 我们的项目 android 项目 只需要一个 api 密钥 有可能吗 或者我团队的所有成员都必须生成 api 密钥才能运行 google 地图 您可
  • 无法在 python 中获取当前 url

    我创建了一个类和方法 如下所示 我需要获取当前页面的网址 但在调用 get full path 时出现错误 class A object def get user request current url request get full p
  • python statsmodels:帮助使用 ARIMA 模型进行时间序列

    statsmodels 的 ARIMA 为我的输出提供了不准确的答案 我想知道是否有人可以帮助我理解我的代码有什么问题 这是一个示例 import pandas as pd import numpy as np import datetim
  • 在 git Push 上硬重置

    我有一个接收后挂钩脚本位于我正在推送的远程存储库上 该脚本执行以下操作git reset hard 像这样的东西 git push opal Counting objects 74 done Delta compression using
  • Haskell 约束不小于实例头

    有些戒指可以配备标准功能 class Ring C a gt EuclideanDomain a where norm a gt Integer 使用此功能 可以通过明显的方式订购戒指 compare x y compare norm x
  • 从 Equals 方法返回 false 而不覆盖

    Write TestEquals class所以主要方法Puzzle3班级版画false 注意 您不能覆盖 TestEquals 类的 equals 方法 public class Puzzle3 public static void ma
  • Haskell:如何评估“1+2”等字符串

    实际上我有一些公式 x y 这是一个String 我设法更换了x y具有特定值的变量 例如 1 2 这仍然是String类型 现在我的表情就像 1 2 所以问题是如何计算字符串类型的表达式并得到结果 PS 我想要某样东西read 可以直接转
  • Android 生物识别回调不适用于 PIN-CODE

    我正在尝试使用指纹和 Pincode 来实现生物识别 指纹工作正常 但是当我尝试使用 pin 码时 会出现带有 pin 码的屏幕 但如果我调用 pin 码检查 则不会调用回调没有指纹 只调用 onAuthenticationSucceede
  • 在SSIS中使用脚本任务导入Json文件

    我是在 SSIS 中使用脚本任务导入 API 的新手 我必须管理一个简单的 API JSON 文件的导入 但这第二个 API JSON 文件有点棘手 我一直在看代码 只是不知道我做错了什么 我的 JSON 文件有一个标头 我需要确定需要循环