to byte[] diff error?
Closed this issue · 4 comments
buyaoyongroot commented
func main() {
jsonParsed, err := gabs.ParseJSON([]byte(`{
"outter":{
"inner":{
"value1": "hello\\r\\nworld",
"value2":22
}
}
}`))
if err != nil {
panic(err)
}
var value string
value, _ = jsonParsed.Path("outter.inner.value1").Data().(string)
fmt.Println(value)
fmt.Println(value == "hello\r\nworld")
alue1 := []byte(value)
value2 := []byte("hello\r\nworld")
fmt.Println(value1)
fmt.Println(value2)
}
hello\r\nworld
false
[104 101 108 108 111 92 114 92 110 119 111 114 108 100]
[104 101 108 108 111 13 10 119 111 114 108 100]
like this demo, I can not get the same string, what can I code it.
buyaoyongroot commented
so I can use this code to this answer, but it looks not beautiful, have a better idea? please
s1, _ := hex.DecodeString("5C725c6E")
s2, _ := hex.DecodeString("0D0A")
value1 = bytes.ReplaceAll(value1, s1, s2)
fmt.Println(value1)
fmt.Println(value2)
mihaitodor commented
Hey @buyaoyongroot, in your input JSON, your string is "hello\\r\\nworld"
, but then you're doing fmt.Println(value == "hello\r\nworld")
. Shouldn't that be fmt.Println(value == "hello\\r\\nworld")
?
buyaoyongroot commented
json data hello\\r\\nworld if success translate hello\r\nworld?
mihaitodor commented
Not sure I follow, but this might be relevant: https://golangbyexample.com/backslash-print-golang/