json.Unmarshal无法工作,即使有一个导出字段。

huangapple go评论86阅读模式
英文:

json.Unmarshal not work even though there is an export field

问题

json文件:

{
  "student_class": [
    {
      "student_id": 1,
      "class_id": 2
    },
    {
      "student_id": 1,
      "class_id": 1
    }
  ]
}

结构体:

package studentClass

type StudentClasses struct {
	StudentClasses []StudentClass `json:"student_class"`
}

type StudentClass struct {
	StudentId int `json:"student_id"`
	ClassId   int `json:"class_id"`
}

我的函数:

func Read() {
	var studentClasses studentClass.StudentClasses
	jsonFile, err := os.Open("db/student_class.json")
	if err != nil {
		fmt.Println(err)
	}
	defer jsonFile.Close()

	byteValue, _ := io.ReadAll(jsonFile)
	json.Unmarshal(byteValue, &studentClasses)

	for i := 0; i < len(studentClasses.StudentClasses); i++ {
		fmt.Println(studentClasses.StudentClasses[i])
	}
}

它没有返回任何内容。

当我在json.Unmarshal之后添加fmt.Println(studentClasses)时,它返回{[]}
json.Unmarshal的错误为nil。

我已经研究了这个问题,但是和我有相同问题的人说结构体的字段没有导出。例如:https://stackoverflow.com/questions/29193556/go-json-unmarshal-do-not-working
我不知道错误出在哪里,我做错了什么。
请帮助我解决这个问题。谢谢大家!

英文:

json file:

{
  &quot;student_class&quot;: [
    {
      &quot;student_id&quot;: 1,
      &quot;class_id&quot;: 2
    },
    {
      &quot;student_id&quot;: 1,
      &quot;class_id&quot;: 1
    },

Struct:

package studentClass

type StudentClasses struct {
	StudentClasses []StudentClass
}

type StudentClass struct {
	StudentId int `json:&quot;student_id&quot;`
	ClassId   int `json:&quot;class_id&quot;`
}

my function:

func Read() {
	var studentClasses studentClass.StudentClasses
	jsonFile, err := os.Open(&quot;db/student_class.json&quot;)
	if err != nil {
		fmt.Println(err)
	}
	defer jsonFile.Close()

	byteValue, _ := io.ReadAll(jsonFile)
	json.Unmarshal(byteValue, &amp;studentClasses)

	for i := 0; i &lt; len(studentClasses.StudentClasses); i++ {
		fmt.Println(studentClasses.StudentClasses[i])
	}

}

It return nothing

When i add fmt.Println(studentClasses) after json.Unmarshall... then it return {[]}
Error off json.Unmarshal is nil

I have researched about this problem but people with the same problem as me are saying that struct's field is not exported. Example: https://stackoverflow.com/questions/29193556/go-json-unmarshal-do-not-working
I do not know where the error is and what am I doing wrong
Please help me to solve this problem. thanks everyone!

答案1

得分: 0

你没有为StudentClasses指定json名称。

type StudentClasses struct {
    StudentClasses []StudentClass `json:"student_class"`
}

示例

package main

import (
    "encoding/json"
    "fmt"
)

type StudentClasses struct {
    StudentClasses []StudentClass `json:"student_class,omitempty"`
}

type StudentClass struct {
    StudentId int `json:"student_id"`
    ClassId   int `json:"class_id"`
}

func main() {
    _json := `{
        "student_class": [
            {
                "student_id": 1,
                "class_id": 2
            },
            {
                "student_id": 1,
                "class_id": 1
            }
        ]
    }`
    var studentClasses StudentClasses
    json.Unmarshal([]byte(_json), &studentClasses)

    fmt.Printf("%+v", studentClasses)
}
英文:

You didn't specify the json name for StudentClasses.

type StudentClasses struct {
    StudentClasses []StudentClass `json:&quot;student_class&quot;`
}

Sample:

package main

import (
	&quot;encoding/json&quot;
	&quot;fmt&quot;
)

type StudentClasses struct {
	StudentClasses []StudentClass `json:&quot;student_class,omitempty&quot;`
}

type StudentClass struct {
	StudentId int `json:&quot;student_id&quot;`
	ClassId   int `json:&quot;class_id&quot;`
}

func main() {
	_json := `{
  &quot;student_class&quot;: [
    {
      &quot;student_id&quot;: 1,
      &quot;class_id&quot;: 2
    },
    {
      &quot;student_id&quot;: 1,
      &quot;class_id&quot;: 1
    }
  ]
}`
	var studentClasses StudentClasses
	json.Unmarshal([]byte(_json), &amp;studentClasses)

	fmt.Printf(&quot;%+v&quot;, studentClasses)
}

huangapple
  • 本文由 发表于 2022年12月18日 01:30:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/74836083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定