将字节数组设置为json.RawMessage。

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

set array of byts to json.RawMessage

问题

我有一个字节数组在golang中:

obj_data, _ := json.Marshal(obj)

我想将这个字节数组设置到json.RawMessage中。
我以为这样会起作用:

data := json.RawMessage{obj_data}

但是我遇到了错误:

无法将obj_data(类型为[]byte)用作数组元素中的byte类型

请帮助我!=)

英文:

I have array of bytes in golang:

obj_data, _ := json.Marshal(obj)

And a want set this array of bytes into json.RawMessage
I thought that it would work:

data := json.RawMessage{obj_data}

but i have error:

cannot use obj_data (type []byte) as type byte in array element

please help me! =)

答案1

得分: 3

你需要进行类型转换,而不是使用字面量:

data := json.RawMessage(obj_data)
英文:

You need a type conversion, not a literal:

data := json.RawMessage(obj_data)

huangapple
  • 本文由 发表于 2016年1月20日 01:01:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/34882694.html
匿名

发表评论

匿名网友

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

确定