0
我想使用ServiceStack.Text反序列化包含自定義標題的csv文件。使用ServiceStack.Text反序列化CSV與CustomHeaders
var csv = "Col-1,Col-2" + Environment.NewLine +
"Val1,Val2" + Environment.NewLine +
"Val3,Val3" + Environment.NewLine;
public class Line
{
public string Col1 { get; set; }
public string Col2 { get; set; }
}
ServiceStack.Text.CsvConfig<Line>.CustomHeadersMap = new Dictionary<string, string> {
{"Col1", "Col-1"},
{"Col2", "Col-2"}
};
var r2 = ServiceStack.Text.CsvSerializer.DeserializeFromString<List<Line>>(csv);
Assert.That(r2.Count() == 2, "It should be 2 rows");
Assert.That(r2[0].Col1 == "Val1", "Expected Val1");
Assert.That(r2[0].Col2 == "Val2", "Expected Val2");
使用SerializeToString時,CustomHeadersMap正在工作。但是在使用DeserializeFromString時我無法正常工作。
的例子有一個錯誤的分離器,它現在應該是正確的。在解串器中使用CustomHeadersMap? –
@ Kai-Rune的示例文字看起來像完全一樣嗎?即沒有接近CSV,我用一個例子更新了我的答案。我也只是[添加了一個提交](https://github.com/ServiceStack/ServiceStack.Text/commit/6085b45e63520f788d1d0b0b748cd2541e37e652),它也將這些自定義標頭應用於CSV解串器。此更改現已在MyGet上的v4.0.57中提供,但您需要[清除MyGet緩存](https://github.com/ServiceStack/ServiceStack/wiki/MyGet#redownloading-myget-packages)以獲取最新包版本。 – mythz
我仍然遇到更新的nuget包的錯誤,請運行問題中的代碼。 csv數據包含在代碼中。 –