2017-05-09 39 views
0

我想使用SSIS(腳本組件和C#代碼與JSON.net庫)來讀取JSON文件。我的JSON文件看起來很複雜,我是C#代碼的新手。以下是關於我的JSON文件外觀的示例。使用SSIS腳本組件讀取JSON文件

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

任何幫助,將不勝感激。

回答

0

你需要像下面的類結構:

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(); 
} 
+0

謝謝Sameer。讓我試試這個 – Varma

+0

無法加載文件或程序集'Newtonsoft.Json,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed'或其依賴項之一。該系統找不到指定的文件。 ---我在執行SSIS包時遇到此錯誤 – Varma

+0

我還沒有解決它。但這給了我一張好照片。當然會做 – Varma