无法将JSON解组到结构中 [英] Unable to unmarshal JSON into struct

查看:97
本文介绍了无法将JSON解组到结构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {MAIN:{data:[{ KEY1: 1111111, KEY2: 2222222, KEY3:0, KEY4: AAAAAAA, KEY5: 9999, 键6: 4, KEY7: BBBBBB}]}} 

我试图修改 jsonStruct ,但结构始终为空:

 包主


encoding / json
fmt


类型jsonStruct struct {
主结构{
data []结构{
Key1字符串`json:KEY1`
Key2字符串`json:KEY2`
Key3 int`json:KEY3`
Key4 string`json: KEY4`
Key5字符串`json:KEY5`
Key6字符串`json:KEY6
Key7字符串`json:KEY7`
}`json :data`
}`json:MAIN`
}

func main(){
jsonData:= [] byte(`{MAIN :{ 数据 :[{ KEY1 : 1111111\" , KEY2:22222 22,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不包含括号,因此我怀疑问题与它们有关。



Can任何人都可以帮忙?



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

解决方案

这是因为其他软件包( encoding / json )不能访问私有字段(即使有反射)。在去,私人领域是以小写字母开头的字段。为了解决这个问题,使你的结构体包含公共字段(以大写字母开头):

  type jsonStruct struct {
主结构{
Data []结构{
Key1字符串`json:KEY1`
Key2字符串`json:KEY2`
Key3 int`json: KEY3`
Key4字符串`json:KEY4`
Key5字符串`json:KEY5`
Key6字符串`json:KEY6
Key7字符串`json :KEY7`
}`json:data`
}`json:MAIN`
}

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


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-

解决方案

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

这篇关于无法将JSON解组到结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆