简体   繁体   English

Golang json。解组字符串文字中的无效字符'\\ n'

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

Golang json.Unmarshal throws error for newline character. Golang json.Unmarshal引发换行符错误。 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. 由于您尝试在此处传递原始字符串文字,因此您将需要能够以字符串形式表示JSON,这要求您转义换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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