csv反序列化_使用ServiceStack.Text使用CustomHeaders反序列化CSV

I'm trying to use ServiceStack.Text for deserializing a csv file containing custom headers.

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.CustomHeadersMap = new Dictionary {

{"Col1", "Col-1"},

{"Col2", "Col-2"}

};

var r2 = ServiceStack.Text.CsvSerializer.DeserializeFromString>(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");

CustomHeadersMap is working when SerializeToString is used. But I can't get it working when using DeserializeFromString.

解决方案

The sample text you're trying to deserialize has very little in common with the Comma-Separated Values (CSV) format that ServiceStack's CSV Format should be used to deserialize.

I'm not aware of any .NET library that can deserialize the text format in your Sample so I'd recommend running it through some a custom regex/normalizer which can convert it to a proper .csv file and deserialize that instead. Here's an example in JavaScript:

var txt = `---------------

| Col-1 | Col-2 |

---------------

| Val1 | Val2 |

---------------

| Val3 | Val4 |

---------------

`;

var csv = txt

.replace(/^-*/mg, '')

.replace(/(^\| | \|$)/mg, '')

.replace(/ \| /mg,',')

.split(/\r?\n/g)

.filter(s => s)

.join('\r\n')

Where csv now contains the string:

Col-1,Col-2

Val1,Val2

Val3,Val4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ResponseEntity自定义Headers的方法如下: 首先,创建一个HttpHeaders对象来设置自定义的Headers。可以使用add()方法来添加具体的Header键值对。例如,可以使用set()方法来设置一个键值对。 然后,创建一个ResponseEntity对象并将要返回的响应体和自定义的Headers传入。可以使用ok()方法来创建一个成功的ResponseEntity对象。 最后,在Controller方法中,将ResponseEntity对象作为返回值返回即可。 下面是一个示例代码: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/customHeaders") public ResponseEntity<String> getResponseWithCustomHeaders() { // 创建自定义的Headers HttpHeaders headers = new HttpHeaders(); headers.add("Custom-Header", "Custom Value"); headers.set("Another-Header", "Another Value"); // 创建ResponseEntity对象并设置自定义Headers ResponseEntity<String> responseEntity = ResponseEntity.ok() .headers(headers) .body("Response Body"); return responseEntity; } } ``` 在上述示例中,通过访问`/customHeaders`路径,将返回一个带有自定义Headers的ResponseEntity对象。其中,自定义Headers包括"Custom-Header"和"Another-Header"两个键值对。 请注意,在使用ResponseEntity时,需要在Controller方法中返回ResponseEntity对象,而不是直接返回响应体内容。通过设置ResponseEntity对象的headers属性来设置自定义Headers。在Spring框架中,可以使用ResponseEntity对象来设置响应头信息。 : 需要注意的是,在使用ResponseEntity时,需要在Controller方法中返回ResponseEntity对象,而不是直接返回响应体内容。 : 在Spring框架中,可以使用ResponseEntity对象来设置响应头信息。可以通过以下代码设置响应头:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值