繁体   English   中英

使用SSIS脚本组件读取JSON文件

[英]Reading JSON file using SSIS script component

我正在尝试使用SSIS(带有JSON.net库的脚本组件和C#代码)读取JSON文件。 我的JSON文件看起来很复杂,并且是C#代码的新手。 以下是有关我的JSON文件外观的示例。

{
    "Product": {
        "col1": "xyz",
        "col2": "ryx"
    },
    "Samples": [{
            "col3": "read",
            "col4": "write"
        },
        {
            "col3": "read",
            "col4": "update"
        }
    ]
}

任何帮助,将不胜感激。

您需要如下所示的类结构:

public class Product
{
    public string col1 { get; set; }
    public string col2 { get; set; }
}

public class Sample
{
    public string col3 { get; set; }
    public string col4 { get; set; }
}

public class Root
{
    public Product Product { get; set; }

    public Sample[] Samples { get; set; }
}

下面是可用于读取JSON的代码。

注意: sample.json文件包含JSON响应。

public static void Main(string[] args)
{

        using (var stream = new StreamReader("sample.json"))
        {

            var sample = JsonConvert.DeserializeObject<Root>(stream.ReadToEnd());
            Console.WriteLine(sample.Product.col1);
            Console.WriteLine(sample.Product.col2);
            foreach (var t in sample.Samples)
            {
                Console.WriteLine(t.col3);
                Console.WriteLine(t.col4);
            }
        }

        Console.Read();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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