I am reading a json document from localhost and trying to convert it to a Test
type:
type Test struct {
one string
two string
three string
}
res, err := http.Get("http://localhost/d/")
perror(err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
perror(err)
var data Test
err = json.Unmarshal(body, &data)
if err != nil {
fmt.Printf("%T\n%s\n%#v\n",err, err, err)
switch v := err.(type){
case *json.SyntaxError:
fmt.Println(string(body[v.Offset - 40:v.Offset]))
}
}
fmt.Println("response:")
fmt.Println(string(body))
fmt.Println("type:")
fmt.Println(data)
But the output shows an empty object:
response:
{
"one" : "one thing",
"two" : "two things",
"three" : "3 things"
}
type:
{ }
What am I doing wrong?