[英]Reading JSON file using SSIS script component
I'm trying to read a JSON file using SSIS (script component and C# code with JSON.net library). 我正在尝试使用SSIS(带有JSON.net库的脚本组件和C#代码)读取JSON文件。 My JSON file looks complex and I'm new to C# code.
我的JSON文件看起来很复杂,并且是C#代码的新手。 Below is the sample on how my JSON file looks like.
以下是有关我的JSON文件外观的示例。
{
"Product": {
"col1": "xyz",
"col2": "ryx"
},
"Samples": [{
"col3": "read",
"col4": "write"
},
{
"col3": "read",
"col4": "update"
}
]
}
Any help would be appreciated. 任何帮助,将不胜感激。
You need class structure like below: 您需要如下所示的类结构:
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; }
}
And below is the code you can use to read the JSON. 下面是可用于读取JSON的代码。
Note: sample.json file contains JSON response. 注意: 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.