使用ServiceStack.Text反序列化CSV [英] Deserialize CSV with ServiceStack.Text

查看:70
本文介绍了使用ServiceStack.Text反序列化CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ServiceStack.Text反序列化包含;"的csv文件.作为分隔符.

测试csv包含此数据

  -------------------|Col1 |Col2 |---------------|Val1 |Val2 |---------------|Val3 |Val4 |---------------公共课线{公共字符串Col1 {get;放;}公共字符串Col2 {get;放;}} 

有效的代码:

  var CsvWithComma ="Col1,Col2" + Environment.NewLine +"Val1,Val2" + Environment.NewLine +"Val3,Val3" + Environment.NewLine;var r1 = ServiceStack.Text.CsvSerializer.DeserializeFromString< List< Line>>(CsvWithComma);Assert.That(r1.Count()== 2,应该为2行"));断言.that(r1 [0] .Col1 =="Val1","Exp1 Val1");断言.That(r1 [0] .Col2 =="Val2",预期Val2"); 

失败的代码:

  ServiceStack.Text.CsvConfig.ItemSeperatorString =;";var CsvWithSemicolon ="Col1; Col2" + Environment.NewLine +"Val1; Val2" + Environment.NewLine +"Val3; Val3" + Environment.NewLine;var r2 = ServiceStack.Text.CsvSerializer.DeserializeFromString< List< Line>>(CsvWithSemicolon);Assert.That(r2.Count()== 2,应该为2行"));断言.That(r2 [0] .Col1 =="Val1",预期的Val1");断言.That(r2 [0] .Col2 =="Val2",预期的Val2"); 

当我深入研究ServiceStack代码时,似乎正在使用ServiceStack.Text.Common.JsWriter.ItemSeperator作为CSV阅读器的分隔符,而不是ServiceStack.Text.CsvConfig.ItemSeperatorString.

有人能做到这一点吗?

解决方案

现在应该从此提交.

此更改可从v4.0.57 +版本开始,该版本现在为可从MyGet获得./p>

I'm trying to use ServiceStack.Text for deserializing a csv file containing ";" as the seperator.

The test csv contains this data

---------------
| Col1 | Col2 |
---------------
| Val1 | Val2 |
---------------
| Val3 | Val4 |
---------------

public class Line
{
    public string Col1 { get; set; }
    public string Col2 { get; set; }
}

The code that works:

var CsvWithComma = "Col1,Col2" + Environment.NewLine + 
"Val1,Val2" + Environment.NewLine +
"Val3,Val3" + Environment.NewLine;

var r1 = ServiceStack.Text.CsvSerializer.DeserializeFromString<List<Line>>(CsvWithComma);

Assert.That(r1.Count() == 2, "It should be 2 rows");
Assert.That(r1[0].Col1 == "Val1", "Expected Val1");
Assert.That(r1[0].Col2 == "Val2", "Expected Val2");

The code that fails:

ServiceStack.Text.CsvConfig.ItemSeperatorString = ";";

var CsvWithSemicolon = "Col1;Col2" + Environment.NewLine +
"Val1;Val2" + Environment.NewLine +
"Val3;Val3" + Environment.NewLine;

var r2 = ServiceStack.Text.CsvSerializer.DeserializeFromString<List<Line>>(CsvWithSemicolon);

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");

When I dig into the ServiceStack code is seems like it's using the ServiceStack.Text.Common.JsWriter.ItemSeperator as a seperator for the CSV reader and not the ServiceStack.Text.CsvConfig.ItemSeperatorString.

Has anyone got this working?

解决方案

This should now be supported from this commit.

This change is available from v4.0.57+ that's now available from MyGet.

这篇关于使用ServiceStack.Text反序列化CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆