Loops 从嵌套结构中检索值

Loops 从嵌套结构中检索值,loops,for-loop,go,range,Loops,For Loop,Go,Range,我试图从golang中的嵌套结构访问值。我的最终结构如下 StudentDetails = [{"rollno":3,"name":"John","score":{"sub1":50,"sub2":48,"sub3":45}} , {"rollno":4,"name":"James","s

我试图从golang中的嵌套结构访问值。我的最终结构如下

StudentDetails = [{"rollno":3,"name":"John","score":{"sub1":50,"sub2":48,"sub3":45}} , {"rollno":4,"name":"James","score":{"sub1":38,"sub2":35,"sub3":40}}]
type scoreCard struct {
    subject1                int     `json:"id"`
    subject2                int     `json:"id"`
    subject3                int     `json:"id"`

}

type StudDetails    struct {

    roll int
    name string
    score scoreCard
}
我想分别打印这些值。为此,我尝试添加一个循环

for i, details  := range StudentDetails {

    glog.Info("increment ", i)
    glog.Info("name of student ", details.name)
    glog.Info("mark of sub 1", details.score.sub1)
}
但我总是犯错误

“(类型映射[字符串]接口{}没有字段或方法名” 我的结构如下

StudentDetails = [{"rollno":3,"name":"John","score":{"sub1":50,"sub2":48,"sub3":45}} , {"rollno":4,"name":"James","score":{"sub1":38,"sub2":35,"sub3":40}}]
type scoreCard struct {
    subject1                int     `json:"id"`
    subject2                int     `json:"id"`
    subject3                int     `json:"id"`

}

type StudDetails    struct {

    roll int
    name string
    score scoreCard
}
您创建的“StudentDetails”变量是map类型的

var StudentDetails=[]StudDetails{{“roll”:3,“name”:“John”,“score”:{“subject1”:50,“subject2”:48,“subject3”:45},{“roll”:4,“name”:“James”,“score”:{“subject1”:38,“subject2”:35,“subject3”:40}


此外,您的结构字段名也存在一些不一致之处;在创建实例时也应使用定义期间使用的字段名,否则您将收到错误。

您的错误表明您实际上没有结构,但有映射。为此,请参阅。请注意,即使您试图正确地解组到结构或结构的一部分s、 正如icza指出的那样,这对您没有帮助,因为您没有导出字段。您可以使用get
json to go struct
进行解组,json封送/解组中使用的任何字段都必须导出。