英文:
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:
{
"student_class": [
{
"student_id": 1,
"class_id": 2
},
{
"student_id": 1,
"class_id": 1
},
Struct:
package studentClass
type StudentClasses struct {
StudentClasses []StudentClass
}
type StudentClass struct {
StudentId int `json:"student_id"`
ClassId int `json:"class_id"`
}
my function:
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])
}
}
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:"student_class"`
}
Sample:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论