简体   繁体   中英

Golang json.Unmarshal invalid character '\n' in string literal

Golang json.Unmarshal throws error for newline character. Go Playground

How to unmarshal data if string contains newline?

Simply escaping the newline character should do the trick:

var val []byte = []byte(`"{\"channel\":\"buupr\\niya\",\"name\":\"john\", \"msg\":\"doe\"}"`)

The output for the above:

{"channel":"buupr\niya","name":"john", "msg":"doe"}

Since you're attempting to pass a raw string literal here, you will need to be able to represent the JSON in string form, which requires you to escape the newline character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM