如何将原始的 CSV 响应解组为结构体?

huangapple go评论71阅读模式
英文:

How to unmarshal raw CSV response into a struct?

问题

我有一个只能获取字节响应的身体响应。这些字节编码了类似CSV的响应。类似于:

element_a,element_b,element_c
cooper,claus,active
carlos,saldanha,inactive
robert,jesus,active

假设我有以下结构体:

type ESResponse struct {
    ElementA string `csv:"element_a"`
    ElementB string `csv:"element_b"`
    ElementC string `csv:"element_c"`
}

我想要将字节响应解组,以便能够访问其元素。

我一直在做以下操作:

var actualResult ESResponse

body := util.GetResponseBody() // 这是字节响应的来源
in := string(body[:]) // 在这里我将其转换为字符串,但我真的认为这不是最好的方法
err = gocsv.Unmarshal(in, &actualResult)

我一直在使用这个库:https://pkg.go.dev/github.com/gocarina/gocsv#section-readme,但我无法理解我得到的错误,错误信息是:

cannot use in (variable of type string) as io.Reader value in argument to gocsv.Unmarshal: string does not implement io.Reader (missing method Read)
英文:

I have a body response that I can only get Byte responses. This bytes encode a csv-like response. Something like:

element_a,element_b,element_c
cooper,claus,active
carlos,saldanha,inactive
robert,jesus,active

Lets say then that I have the struct that looks like this:

type ESResponse struct {
ElementA string `csv:"element_a"`
ElementB string `csv:"element_b"`
ElementC string `csv:"element_c"`
}

I would like to unmarshal the byte response so then I'm able to access its elements.

What I've been doing is the following:

var actualResult ESResponse

body := util.GetResponseBody() // this is where the byte response comes from. 
in := string(body[:]) // here I transform it to a string but I trully think this is not the best way. 
err = gocsv.Unmarshal(in, &actualResult)

I've been using this library here: https://pkg.go.dev/github.com/gocarina/gocsv#section-readme but I'm unable to understand the error I get which is:

cannot use in (variable of type string) as io.Reader value in argument to gocsv.Unmarshal: string does not implement io.Reader (missing method Read)

答案1

得分: 2

这意味着in参数必须实现io.Reader接口,但你的参数类型是字符串,而字符串并没有实现该接口。所以如果你想从字符串中反序列化数值,你可以这样做:

	body := `
element_a,element_b,element_c
cooper,claus,active
carlos,saldanha,inactive
robert,jesus,active`
	var actualResult []ESResponse
	in := strings.NewReader(body)
	err := gocsv.Unmarshal(in, &actualResult)

或者使用gocsv.Unmarshal(bytes.NewReader([]byte(body)), &actualResult)从字节数组中反序列化。

英文:

It means, that in argument must implement interface io.Reader, but you argument's type is string, which doesn't. So if you want to deserialize value from string, you can do this:

	body := `
element_a,element_b,element_c
cooper,claus,active
carlos,saldanha,inactive
robert,jesus,active`
	var actualResult []ESResponse
	in := strings.NewReader(body)
	err := gocsv.Unmarshal(in, &actualResult)

or gocsv.Unmarshal(bytes.NewReader([]byte(body)), &actualResult) to deserialize from bytes array

huangapple
  • 本文由 发表于 2022年12月22日 03:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/74881026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定