I need to decode a JSON string which has "\n" in it:
[
{"Name":"Neo", "Message":"Hi\n:Hello everyone"},
{"Name":"Sam","Messsage":"Hello\nEveery\nOne"}
]
I use the Golang code below:
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Messages []string `json:"Name,omitempty"`
}
func main() {
s := "[{\"Name\":\"Neo\", \"Message\":\"Hi\n:Hello everyone\"}, {\"Name\":\"Sam\",\"Messsage\":\"Hello\nEveery\nOne\"}]"
var pro Person
err := json.Unmarshal([]byte(s), &pro)
if err == nil {
fmt.Printf("%+v\n", pro)
} else {
fmt.Println(err)
fmt.Printf("%+v\n", err)
}
}
But I get the error:
ERROR invalid character '\n' in string literal
"[{\"Message\": \"Hi\\n:Hello\"}]. But you should really use this syntax`[{"Message": "Hi\n:Hello"}]`for readability, as suggested in the play link in earlier comment.