2016-02-13 57 views
0

任何幫助,將不勝感激。我試圖解析SSIS中的JSON源文件(SQL Server Integration Services)中的數據。我可以通過數據解析,但是在解析數據時存在'一對多'關係。有一個數據實體反覆幾次(「display_k」) -C#:JSON一對多數據分析 - SSIS腳本組件

"responses": 
[ 
{"display_k":"good","answer":null} 
,{"display_k":"bad","answer":null} 
,{"display_k":"general","answer":"The whole process was Easy. "} 
,{"display_k":"would_buy_again","answer":true} 
,{"display_k":"happy_with_customer_service","answer":null} 
] 

完整的代碼如下:

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 
using Microsoft.SqlServer.Dts.Runtime.Wrapper; 
using System.IO; 
//using Newtonsoft.Json; 
using System.Collections.Generic; 
using System.Runtime.Serialization.Json; 
using System.Text; 

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 
    public override void CreateNewOutputRows() 
    { 
     var filePath = Connections.Connection.AcquireConnection(null).ToString(); 
     using (var fileContents = new StreamReader(filePath)) 

      while (fileContents.Peek() >= 0) 
      { 
       var record = fileContents.ReadLine(); 

       var ser = new DataContractJsonSerializer(typeof(RootObject)); 
       var memStream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(record)); 
       var root = ser.ReadObject(memStream) as RootObject; 

       //reviewables 
       var CustomerExperienceReview = root.customer_experience_reviews; 
       foreach (var CER in CustomerExperienceReview) 
       { 
        OutputBuffer.AddRow(); 

        OutputBuffer.id = CER.id; 
        OutputBuffer.branchattribution = CER.branch_attribution; 
        OutputBuffer.reviewerfirstname = CER.reviewer.first_name; 
        OutputBuffer.reviewerid = CER.reviewer.id; 

//不能得到的輸出緩衝器,以顯示正確的結果:

OutputBuffer.responsesdisplaykey = string.Join(",", CER.responses.display_key); 
       } 
      } 
    } 

    public class Pagination 
    { 
     public int total_entries { get; set; } 
     public int current_page { get; set; } 
     public int total_pages { get; set; } 
     public int per_page { get; set; } 
     public object previous_page { get; set; } 
     public int next_page { get; set; } 
    } 

    public class Summary 
    { 
     public Pagination pagination { get; set; } 
     public int moving_window_size { get; set; } 
     public SortOrder sort_order { get; set; } 
     public List<object> sort_orders { get; set; } 
    } 

    public class Reviewer 
    { 
     public string first_name { get; set; } 
     public int id { get; set; } 
    } 

    public class Respons 
    { 
     public string display_key { get; set; } 
     public object answer { get; set; } 
    } 

    public class CustomerExperienceReview 
    { 
     public int id { get; set; } 
     public string branch_attribution { get; set; } 
     public List<Respons> responses { get; set; } 


    } 

    public class RootObject 
    { 
     public Summary summary { get; set; } 
     public List<CustomerExperienceReview> customer_experience_reviews { get; set; } 
    } 
} 

回答

1

您有:

OutputBuffer.responsesdisplaykey = string.Join(",", CER.responses.display_key); 

根據類別定義,CER.responses是List <Respons>。如果您希望顯示鍵的逗號分隔列表,則需要將響應對象投影到字符串。

OutputBuffer.responsesdisplaykey = string.Join(",", CER.responses.Select(r=>r.display_key)); 
+0

嗨克里斯感謝您的回覆。我收到以下錯誤消息:錯誤'System.Collections.Generic.List '沒有包含'Select'的定義,也沒有包含接受類型'System'的第一個參數的擴展方法'Select' .Collections.Generic.List '可以找到(你是否缺少使用指令或程序集引用?) – Aarion

+0

我已經添加使用System.Linc 我得到display_key ...有關如何我可以得到「回答」的好評嗎? 謝謝你的幫助:) – Aarion

+0

請忽略我最後的評論。我對C-sharp腳本缺乏信心。我有它的工作: OutputBuffer.responsesdisplaykey = string.Join(「,」,CER.responses.Select(r => r.display_key +「:」+ r.answer)); – Aarion