Json Marshal - escaping string char

2,574 views
Skip to first unread message

bsr

unread,
Nov 22, 2012, 12:36:05 PM11/22/12
to golan...@googlegroups.com
Hello,

How to treat an embedded json string as it, and not to escape it during json marshal.


this code explains what I am trying to do (this example is not working after I change to use a struct).
I have one field(r), which I am setting a json string. I marshal it as 

j, err := json.Marshal(map[string]interface{}{
"message": r.m,
"reply":   r.r,
})


At client, I get all the " escaped as \" shown in the first sample. 

{"message":null,"reply":"{\"6\":[{\"Id\":7,\"Org

{"message":null,"reply":[{"Id":2,

But, if I directly encode the object, I get the response as in second sample. I can make it to work at the client by parsing it in JS.
JSON.parse(response)

Do you think it is more efficient to pass the un-escaped string. Is it possible?





Dustin Sallings

unread,
Nov 22, 2012, 5:59:19 PM11/22/12
to golan...@googlegroups.com
bsr <bsr...@gmail.com> writes:

> Hello,
>
>
> How to treat an embedded json string as it, and not to escape it
> during json marshal.
>
> http://play.golang.org/p/JXL3a9YZ_A

Is this close enough?

http://play.golang.org/p/gFaH0iSc9D

--
dustin

bsr

unread,
Nov 22, 2012, 7:53:15 PM11/22/12
to golan...@googlegroups.com
Dustin. Excellent. Feel bad that I didn't check the package doc thoroughly. Thanks again.
Reply all
Reply to author
Forward
0 new messages