You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Go语言JSON反序列化结构体字段为空问题求助

Go JSON反序列化中Answers/CorrectAnswers字段为空的解决方法

你的问题出在结构体字段与JSON键名不匹配,Go标准库的json.Unmarshal无法自动将JSON中的answer_a、answer_a_correct这类键映射到你结构体里的A、B字段上,必须通过结构体标签显式指定对应关系。

核心修正:添加JSON结构体标签

给结构体中所有与JSON键名不直接匹配的字段(尤其是嵌套的Answers和CorrectAnswers子字段)添加json:"键名"标签,明确映射关系:

type Data struct {
    ID                      int
    Question                string
    Description             string `json:"description"`
    Answers                 struct {
        A string `json:"answer_a"`
        B string `json:"answer_b"`
        C string `json:"answer_c"`
        D string `json:"answer_d"`
        E string `json:"answer_e"`
        F string `json:"answer_f"`
    } `json:"answers"`
    MultipleCorrectAnswers  string `json:"multiple_correct_answers"`
    CorrectAnswers          struct {
        A string `json:"answer_a_correct"`
        B string `json:"answer_b_correct"`
        C string `json:"answer_c_correct"`
        D string `json:"answer_d_correct"`
        E string `json:"answer_e_correct"`
        F string `json:"answer_f_correct"`
    } `json:"correct_answers"`
    CorrectAnswer           string `json:"correct_answer"`
    Explanation             string `json:"explanation"`
    Tip                     string `json:"tip"`
    Tags                    []struct {
        Name string `json:"name"`
    } `json:"tags"`
    Category                string `json:"category"`
    Difficulty              string `json:"difficulty"`
}

完整修正后的FetchQuiz函数

func FetchQuiz(num int, category string) {
    jsonFile, err := os.Open("test.json")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer jsonFile.Close()

    byteValue, err := ioutil.ReadAll(jsonFile)
    if err != nil {
        fmt.Println(err)
        return
    }

    type Data struct {
        ID                      int
        Question                string
        Description             string `json:"description"`
        Answers                 struct {
            A string `json:"answer_a"`
            B string `json:"answer_b"`
            C string `json:"answer_c"`
            D string `json:"answer_d"`
            E string `json:"answer_e"`
            F string `json:"answer_f"`
        } `json:"answers"`
        MultipleCorrectAnswers  string `json:"multiple_correct_answers"`
        CorrectAnswers          struct {
            A string `json:"answer_a_correct"`
            B string `json:"answer_b_correct"`
            C string `json:"answer_c_correct"`
            D string `json:"answer_d_correct"`
            E string `json:"answer_e_correct"`
            F string `json:"answer_f_correct"`
        } `json:"correct_answers"`
        CorrectAnswer           string `json:"correct_answer"`
        Explanation             string `json:"explanation"`
        Tip                     string `json:"tip"`
        Tags                    []struct {
            Name string `json:"name"`
        } `json:"tags"`
        Category                string `json:"category"`
        Difficulty              string `json:"difficulty"`
    }

    var QuizList2 []Data

    if err := json.Unmarshal(byteValue, &QuizList2); err != nil {
        fmt.Println(err.Error())
        return
    }

    // 用%+v格式化输出,可显示字段名与对应值,便于调试
    fmt.Printf("%+v\n", QuizList2)
}

额外注意事项

  • 完善错误处理:补充ioutil.ReadAll的错误检查,避免因文件读取失败导致后续逻辑异常。
  • 调试输出:使用fmt.Printf("%+v", ...)替代fmt.Println,能更清晰地看到结构体的字段名和对应值,便于验证反序列化结果。

内容的提问来源于stack exchange,提问作者Harsh Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 23:35:28