Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to byte[] diff error? #130

Closed
buyaoyongroot opened this issue Dec 8, 2022 · 4 comments
Closed

to byte[] diff error? #130

buyaoyongroot opened this issue Dec 8, 2022 · 4 comments

Comments

@buyaoyongroot
Copy link

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
Copy link
Author

buyaoyongroot commented Dec 8, 2022

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
Copy link
Contributor

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
Copy link
Author

buyaoyongroot commented Dec 20, 2022

json data hello\\r\\nworld if success translate hello\r\nworld?

@mihaitodor
Copy link
Contributor

Not sure I follow, but this might be relevant: https://golangbyexample.com/backslash-print-golang/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants