无法将 JSON 反序列化为结构体。

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

Unable to unmarshal JSON into struct

问题

我想将以下JSON解组成一个结构体:

{"MAIN":{"data":[{"KEY1":"1111111","KEY2":"2222222","KEY3":0,"KEY4":"AAAAAAA","KEY5":"9999","KEY6":"4","KEY7":"BBBBBBB"}]}}

我尝试以各种方式修改jsonStruct,但结构体始终为空:

package main

import (
    "encoding/json"
    "fmt"
)

type jsonStruct struct {
    Main struct {
        Data []struct {
            Key1 string `json:"KEY1"`
            Key2 string `json:"KEY2"`
            Key3 int    `json:"KEY3"`
            Key4 string `json:"KEY4"`
            Key5 string `json:"KEY5"`
            Key6 string `json:"KEY6"`
            Key7 string `json:"KEY7"`
       } `json:"data"`
    } `json:"MAIN"`
}

func main() {
    jsonData := []byte(`{"MAIN":{"data":[{"KEY1":"1111111","KEY2":"2222222","KEY3":0,"KEY4":"AAAAAAA","KEY5":"9999","KEY6":"4","KEY7":"BBBBBBB"}]}}`)

    var js jsonStruct

    err := json.Unmarshal(jsonData, &js)
    if err != nil {
            panic(err)
    }

    fmt.Println(js)
}

输出结果为:

{{[]}}

我之前处理的JSON中没有括号,所以我怀疑问题与括号有关。

有人可以帮忙吗?

链接:https://play.golang.org/p/pymKbOqcM-

英文:

I want to unmarshal the following JSON into a struct:

{"MAIN":{"data":[{"KEY1":"1111111","KEY2":"2222222","KEY3":0,"KEY4":"AAAAAAA","KEY5":"9999","KEY6":"4","KEY7":"BBBBBBB"}]}}

I have tried to modify the jsonStruct in various ways, but the struct is always empty:

package main

import (
    "encoding/json"
    "fmt"
)

type jsonStruct struct {
    main struct {
        data []struct {
            Key1 string `json:"KEY1"`
            Key2 string `json:"KEY2"`
            Key3 int    `json:"KEY3"`
            Key4 string `json:"KEY4"`
            Key5 string `json:"KEY5"`
            Key6 string `json:"KEY6"`
            Key7 string `json:"KEY7"`
       } `json:"data"`
    } `json:"MAIN"`
}

func main() {
    jsonData := []byte(`{"MAIN":{"data":[{"KEY1":"1111111","KEY2":"2222222","KEY3":0,"KEY4":"AAAAAAA","KEY5":"9999","KEY6":"4","KEY7":"BBBBBBB"}]}}`)

    var js jsonStruct

    err := json.Unmarshal(jsonData, &js)
    if err != nil {
            panic(err)
    }

    fmt.Println(js)
}

Output:

{{[]}}

The JSON I have worked with in the past contained no brackets, so I suspect that the problem is related to them.

Can anyone help?

https://play.golang.org/p/pymKbOqcM-

答案1

得分: 8

这是因为其他包(encoding/json)无法访问私有字段(即使使用反射也不行)。在Go语言中,私有字段是以小写字母开头的字段。要解决这个问题,将你的结构体中的字段改为公共字段(以大写字母开头):

type jsonStruct struct {
    Main struct {
        Data []struct {
            Key1 string `json:"KEY1"`
            Key2 string `json:"KEY2"`
            Key3 int    `json:"KEY3"`
            Key4 string `json:"KEY4"`
            Key5 string `json:"KEY5"`
            Key6 string `json:"KEY6"`
            Key7 string `json:"KEY7"`
       } `json:"data"`
    } `json:"MAIN"`
}

链接:https://play.golang.org/p/lStXAvDtpZ

英文:

This is happening because other packages (encoding/json) can't access private fields (even with reflection). In go, private fields are fields starting with a lower case character. To fix this, make your struct contains public fields (which start with an upper case letter):

type jsonStruct struct {
    Main struct {
        Data []struct {
            Key1 string `json:"KEY1"`
            Key2 string `json:"KEY2"`
            Key3 int    `json:"KEY3"`
            Key4 string `json:"KEY4"`
            Key5 string `json:"KEY5"`
            Key6 string `json:"KEY6"`
            Key7 string `json:"KEY7"`
       } `json:"data"`
    } `json:"MAIN"`
}

https://play.golang.org/p/lStXAvDtpZ

huangapple
  • 本文由 发表于 2017年1月28日 02:32:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/41900724.html
匿名

发表评论

匿名网友

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

确定