2012-07-26 28 views
10

我試圖設置一個SSIS作業,它將從MailChimp中提取JSON編碼的郵件列表,將它與我們的CRM數據庫(SQL Server)中的客戶列表進行比較,並通過JSON上傳尚未存在的新客戶。我似乎無法找到關於在SSIS中序列化/反序列化JSON的任何內容,除了編寫腳本任務之外,似乎我無法將.Net序列化庫導入到腳本中。有什麼建議麼?提前致謝!解析SQL Server Integration Services包中的JSON數據?

+1

爲什麼你不能導入序列化類?有人似乎已經[已經完成](http://timlaqua.com/2011/07/consuming-an-authenticated-json-feed-with-ssis/)使用腳本組件。 – Pondlife 2012-07-26 21:24:26

+0

我無法導入包含我嘗試使用的JavaScriptSerializer類的System.Web.Script命名空間;我假設你可以在腳本任務(?)中輸入什麼樣的限制。但是,您提到的那篇文章提供了一個解決方案,即導入System.Runtime.Serialization.Json。我曾看過那篇文章,但讀得太快,錯過了這一點 - 感謝您將它帶回我的注意。 – 2012-07-27 13:37:34

+0

不應該有對導入的限制,但可以通過GAC和SQL Server SDK文件夾添加任何未顯示的內容。這也是您將自己的自定義DLL添加到包的方式。 – GShenanigan 2013-04-26 11:57:52

回答

17

幾件事情要解決這裏:

首先,在腳本組件添加新的庫您的問題。我假設你使用VS 2008來完成你的SSIS開發,並且希望使用.net 3.5庫來完成這個任務。你去項目,添加引用,你沒有看到任何你需要的DLL。這可能部分是因爲你使用的是Windows 7和compact 3.5框架。 .net 3.5.1隨Windows 7一起提供,您只需啓用它即可。轉到控制面板,程序和功能。在該屏幕中,您會看到打開或關閉Windows功能,點擊它。在該窗口中檢查Microsoft .NET Framework 3.5.1,這種方式需要幾分鐘才能運行。完成後,查找類似於這些C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v3.5 \ Profile \ Client和C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \框架\ v3.5版本。在這兩個目錄之間,您會發現任何需要用於JSON序列化/反序列化的dll。這些可以添加到您的項目,通過轉到項目 - >添加引用 - >瀏覽選項卡,然後導航到v3.5目錄並選擇你需要的DLL(System.Web.Extensions.dll(v3.5.30729.5446 )在這個例子中使用)。

要從Web服務中獲取JSON,對其進行反序列化並將數據發送到CRM數據庫,必須在數據流中使用腳本組件作爲源,並將列添加到將要使用的輸出緩衝區中保存來自JSON提要的數據(在輸入和輸出屏幕上)。在代碼中,您將需要重寫CreateNewOutputRows方法。這裏是如何做到這一點的例子:

說你的JSON看起來像這樣... [{"CN":"ALL","IN":"Test1","CO":0,"CA":0,"AB":0},{"CN":"ALL","IN":"Test2","CO":1,"CA":1,"AB":0}]

我的拳頭定義一個類來反映這個JSON提要屬性(和你在輸入中定義的列和輸出屏幕),一旦你反序列化,最終將持有這些價值觀......這樣:

class WorkGroupMetric 
{ 
    public string CN { get; set; } 

    public string IN { get; set; } 

    public int CO { get; set; } 

    public int CA { get; set; } 

    public int AB { get; set; } 
} 

現在你需要打電話給你的web服務,並使用HttpWebRequest和一個流得到JSON提要:

string wUrl = "YOUR WEB SERVICE URI"; 
string jsonString; 
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl); 
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse(); 
Stream responseStream = httpWResp.GetResponseStream(); 
using (StreamReader reader = new StreamReader(responseStream)) 
      { 
       jsonString = reader.ReadToEnd(); 
       reader.Close(); 
      } 

現在我們反序列化我們的JSON到WorkGroupMetric

JavaScriptSerializer sr = new JavaScriptSerializer(); 
WorkGroupMetric[] jsonResponse = sr.Deserialize<WorkGroupMetric[]>(jsonString); 

數組反序列化之後,我們現在可以輸出的行輸出緩衝區:

foreach (var metric in jsonResponse) 
     { 

      Output0Buffer.AddRow(); 
      Output0Buffer.CN = metric.CN; 
      Output0Buffer.IN = metric.IN; 
      Output0Buffer.CO = metric.CO; 
      Output0Buffer.CA = metric.CA; 
      Output0Buffer.AB = metric.AB; 
     } 

這裏是所有的代碼放在一起會像(I有一步步示例here):

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 
using Microsoft.SqlServer.Dts.Runtime.Wrapper; 
using System.Net; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.Windows.Forms; 
using System.IO; 
using System.Web.Script.Serialization; 


[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 

public override void CreateNewOutputRows() 
{ 
    string wUrl = "YOUR WEB SERVICE URI"; 

    try 
    { 
     WorkGroupMetric[] outPutMetrics = getWebServiceResult(wUrl); 

     foreach (var metric in outPutMetrics) 
     { 

      Output0Buffer.AddRow(); 
      Output0Buffer.CN = metric.CN; 
      Output0Buffer.IN = metric.IN; 
      Output0Buffer.CO = metric.CO; 
      Output0Buffer.CA = metric.CA; 
      Output0Buffer.AB = metric.AB; 
     } 

    } 
    catch (Exception e) 
    { 
     failComponent(e.ToString()); 
    } 

} 


private WorkGroupMetric[] getWebServiceResult(string wUrl) 
{ 

    HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl); 
    HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse(); 
    WorkGroupMetric[] jsonResponse = null; 

    try 
    { 

     if (httpWResp.StatusCode == HttpStatusCode.OK) 
     { 

      Stream responseStream = httpWResp.GetResponseStream(); 
      string jsonString; 

      using (StreamReader reader = new StreamReader(responseStream)) 
      { 
       jsonString = reader.ReadToEnd(); 
       reader.Close(); 
      } 

      JavaScriptSerializer sr = new JavaScriptSerializer(); 
      jsonResponse = sr.Deserialize<WorkGroupMetric[]>(jsonString); 

     } 

     else 
     { 
      failComponent(httpWResp.StatusCode.ToString()); 

     } 
    } 
    catch (Exception e) 
    { 
     failComponent(e.ToString()); 
    } 
    return jsonResponse; 

} 

private void failComponent(string errorMsg) 
{ 
    bool fail = false; 
    IDTSComponentMetaData100 compMetadata = this.ComponentMetaData; 
    compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail); 

} 
} 
class WorkGroupMetric 
{ 
public string CN { get; set; } 

public string IN { get; set; } 

public int CO { get; set; } 

public int CA { get; set; } 

public int AB { get; set; } 
} 

這現在可以用作用於數據desti輸入國家(您的CRM數據庫)。在那裏,您可以使用SQL來比較數據並發現不匹配,將數據發送到另一個腳本組件以序列化,並將所需的任何更新發送回Web服務。

OR

你可以在腳本組件的一切,而不是將數據輸出到輸出緩衝器。在這種情況下,您仍然需要對JSON進行反序列化,但將數據放入某種集合中。然後使用實體框架和LINQ來查詢你的數據庫和集合。確定不匹配的內容,對其進行序列化,並將其發送到同一腳本組件中的Web服務。

+0

我一般都有些矛盾的感覺,因爲人們在這裏爲他人做所有的工作,但是對於一個非常徹底的答案+1!很有幫助 :) – 2014-01-20 12:29:40

相關問題