任何帮助将不胜感激。我试图解析SSIS(SQL Server Integration Services)中的JSON源文件中的数据。我可以解析数据,但是在解析存在“一对多”关系的数据时会遇到困难。有几次重复的数据实体(“display_k”) -
"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; }
}
}
答案 0 :(得分:1)
你有:
+----------------------------+
| output |
+----------------------------+
| You are only young once, b |
| ut you can stay immature i |
| ndefinitely. |
+----------------------------+
根据类定义,CER.responses是List&lt; Respons&gt;。如果您想要以逗号分隔的显示键列表,则需要将Respons对象投影到字符串。
OutputBuffer.responsesdisplaykey = string.Join(",", CER.responses.display_key);