2017-05-09 7 views
0

SSIS(JSON.netライブラリでスクリプトコンポーネントとC#コード)を使用してJSONファイルを読み取ろうとしています。私のJSONファイルは複雑に見えますが、私はC#コードが初めてです。以下は、JSONファイルの外観のサンプルです。SSISスクリプトコンポーネントを使用して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を読み込むために使用できるコードは次のとおりです。

答えて

0

は、あなたは以下のようなクラス構造を必要とします。

注: 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

ありがとうございます。これを試してみましょう – Varma

+0

ファイルまたはアセンブリ 'Newtonsoft.Json、Version = 6.0.0.0、Culture = neutral、PublicKeyToken = 30ad4fe6b2a6aeed'またはその依存関係の1つを読み込めませんでした。システムは、指定されたファイルを見つけることができません。 --- SSISパッケージの実行中にこのエラーが表示される – Varma

+0

まだ解決していません。しかし、これは私に良いイメージを与えます。確かになるでしょう – Varma

関連する問題