Unmarshal data in Go using stripe library

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

Unmarshal data in Go using stripe library

问题

刚开始使用Go进行编程。

我正在尝试使用Stripe库。有一个名为Charge的类型,我试图使用它们自定义的UnmarshalJSON方法,但是我传递的任何参数似乎都不起作用。这是怎么回事?

var charge, err = sc.Charges.Get(id, nil)

if err != nil {
    log.Fatal(err)
}

thing := charge.UnmarshalJSON([]byte(charge))

这是函数的链接:https://github.com/stripe/stripe-go/blob/master/charge.go#L162

我收到以下错误信息:
./hello.go:48: cannot convert charge (type *stripe.Charge) to type []byte

我还没有找到满足该函数的参数。感谢任何帮助。

英文:

Just started hacking with Go.

I'm trying to use the Stripe library. There is a type Charge which I am trying to use their custom UnmarshalJSON call, but no argument I pass seems to work. What's going on here?

var charge, err = sc.Charges.Get(id, nil)

if err != nil {
	log.Fatal(err)
}

thing := charge.UnmarshalJSON([]byte(charge))

Here's the function: https://github.com/stripe/stripe-go/blob/master/charge.go#L162

I receive:
./hello.go:48: cannot convert charge (type *stripe.Charge) to type []byte

Haven't found an argument to pass that will satisfy this function yet. Any help appreciated thanks.

答案1

得分: 2

JSON转换为Charge

Charge.UnmarshalJSON()方法期望一个字节切片([]byte)作为参数。Charge是一个结构体类型,你不能将其值(或指向它的指针)直接转换为[]byte。这就是错误信息告诉你的内容。

UnmarshalJSON()方法期望的字节切片是一个描述Charge的JSON文本,或者根据UnmarshalJSON()的实现,它还接受一个单独的JSON文本作为Charge的ID。

所以以下代码应该可以工作:

var charge, err = sc.Charges.Get(id, nil)

if err != nil {
    log.Fatal(err)
}

err := charge.UnmarshalJSON([]byte(`"123"`))

或者使用表示为JSON的Charge结构体(不完整):

var charge, err = sc.Charges.Get(id, nil)

if err != nil {
    log.Fatal(err)
}

s := `{"id":"123","amount":2000,"description":"testing"}`
err := charge.UnmarshalJSON([]byte(s))

fmt.Printf("%+v", thing)

输出应该包含这些字段从JSON文本中正确设置的值,以及其他字段的零值:

{Amount:2000 Desc:testing ID:123}

Charge转换为JSON

要打印一个格式良好的Charge值的JSON表示,可以使用json.Marshal()

out, err := json.Marshal(c)
if err != nil {
    panic(err) // 处理错误
}
fmt.Println(string(out))

或者使用json.MarshalIndent()

out, err := json.MarshalIndent(c, "", "  ")
if err != nil {
    panic(err) // 处理错误
}
fmt.Println(string(out))

示例输出(已删除部分内容):

{
  "amount": 2000,
  "description": "testing",
  "id": "123"
}
英文:

JSON to Charge

Charge.UnmarshalJSON() expects a byte slice ([]byte). Charge is a struct type, you can't convert a value of it (nor a pointer to it) to []byte. That's what the error message tells you.

The byte slice that is expected by the UnmarshalJSON() method is a JSON text describing a Charge, or looking into the implementation of UnmarshalJSON(), it also accepts a single JSON text being the Charge ID.

So this should work:

var charge, err = sc.Charges.Get(id, nil)

if err != nil {
    log.Fatal(err)
}

err := charge.UnmarshalJSON([]byte(`"123"`))

Or a Charge struct represented in JSON (incomplete):

var charge, err = sc.Charges.Get(id, nil)

if err != nil {
    log.Fatal(err)
}

s := `{"id":"123","amount":2000,"description":"testing"}`
err := charge.UnmarshalJSON([]byte(s))

fmt.Printf("%+v", thing)

Output should contain these fields properly set from the JSON text, amongst other fields having their zero values:

{Amount:2000 Desc:testing ID:123}

Charge to JSON

To print a nicely formatted JSON representation of a Charge value, use json.Marshal():

out, err := json.Marshal(c)
if err != nil {
	panic(err) // handle error
}
fmt.Println(string(out))

Or use json.MarshalIndent():

out, err := json.MarshalIndent(c, "", "  ")
if err != nil {
	panic(err) // handle error
}
fmt.Println(string(out))

Example output (stripped):

{
  "amount": 2000,
  "description": "testing",
  "id": "123",
}

huangapple
  • 本文由 发表于 2017年1月22日 12:19:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/41787301.html
匿名

发表评论

匿名网友

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

确定