Sending Json Data with different types

33 views
Skip to first unread message

Ramki...@hotmail.com

unread,
Jan 19, 2020, 12:28:54 AM1/19/20
to golang-nuts
I have a server that requires data to be sent back that looks like this

{"jsonrpc": "2.0", "result": [false, "", "", ""], "id": 1}

Using json Encoder and Json Marshalling, the closes I have come up with is 

{
   "id": 1,
   "jsonrpc": "2.0",
   "result": "[false,\"\",\"\",\"\"]"
}

Is this possible to do? 

burak serdar

unread,
Jan 19, 2020, 1:00:47 AM1/19/20
to Ramki...@hotmail.com, golang-nuts
On Sat, Jan 18, 2020 at 10:28 PM <Ramki...@hotmail.com> wrote:
>
> I have a server that requires data to be sent back that looks like this
>
> {"jsonrpc": "2.0", "result": [false, "", "", ""], "id": 1}

https://play.golang.org/p/WYrvolLytd9

Use a []interface{} for "result".

>
> Using json Encoder and Json Marshalling, the closes I have come up with is
>
> {
> "id": 1,
> "jsonrpc": "2.0",
> "result": "[false,\"\",\"\",\"\"]"
> }
>
> Is this possible to do?
> https://play.golang.org/p/m936B_asGai
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/38e23652-f595-4eb2-892b-39e09021b4fc%40googlegroups.com.

Dan Kortschak

unread,
Jan 19, 2020, 1:03:47 AM1/19/20
to Ramki...@hotmail.com, golang-nuts
Try something like this: https://play.golang.org/p/hcI8eMo08Wx

Your array type needs to be an interface type to allow the different
value types.
Reply all
Reply to author
Forward
0 new messages