简体   繁体   English

json.Unmarshall无法正常工作

[英]json.Unmarshall not working properly

I am reading a json document from localhost and trying to convert it to a Test type: 我正在从本地主机读取json文档,并尝试将其转换为Test类型:

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? 我究竟做错了什么?

您必须导出struct字段,使它们以大写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM