[英]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.