I have a json.Unmarshal in a Golang file which decodes JSON data sent from a Swift program. The code properly decodes r.Body into a String (newStr is the exact same as the string I redeclare it as). However, the json.Unmarshal line doesn't seem to work for no good reason, and even my teacher can't figure it out. Any ideas?
The code:
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
newStr := buf.String()
fmt.Println(newStr)
var data Teacher
newStr = `{"body":"How are you all today?","id":283,"title":"Hello World","userID":238"}`
json.Unmarshal([]byte(newStr), &data)
fmt.Println(data.body)
My struct:
// Teacher struct:
type Teacher struct {
body string `json:"body"`
id int `json:"id"`
title string `json:"title"`
userID int `json:"userID"`
}
Any ideas? I'm totally lost. Thanks!