dsfgds4215 2016-05-28 12:41
浏览 71
已采纳

嵌套JSON解组与二维切片成结构无法在golang中工作

I have a json structure that looks like

{
"devices": [
    {
        "server": {
            "bu": {
                "add_info": false,
                "applications": [
                    [
                        "Systems",
                        12
                    ],
                    [
                        "SomeProject",
                        106
                    ]
                ],
                "name": [
                    [
                        "SomeName",
                        4
                    ],
                    [
                        "CommonName",
                        57
                    ]
                ],
                "owners": [
                    "SomeOwner1",
                    "SomeOwner2"
                ],
                "users": [
                    "SomeUser1",
                    "SomeUser2"
                ]
            }
        }
    }
  ]
}

I am trying to add it to a struct, and the struct looks like

type TwoD [][]string
type MainContainer struct {
    Devices []struct{
        Server struct{
            Bu struct{
                Add_info string `json:"add_info"`
                Applications TwoD `json:"applications"`
                Name TwoD `json:"name"`
                Owners []string `json:"owners"`
                Users []string `json:"users"`
               } `json:"bu"`
               } `json:"server"`
    } `json:"devices"`
}

However, when I am printing the struct, I get only one value from the 2D slice and nothing beyond it.

func main() {
jsonfile, err  := ioutil.ReadFile("./search.json")
if err != nil {
    fmt.Println(err)
    os.Exit(1)
}
var jsonobject MainContainer
json.Unmarshal(jsonfile, &jsonobject)
fmt.Printf("%v", jsonobject)
}

{[{{{ [[Systems ]] [] [] []}}}]}

But if I am omitting the 2d slices in the struct like

type MainContainer struct {
Devices []struct{
        Server struct{
            Bu struct{
                Add_info string `json:"add_info"`
                //Applications TwoD `json:"applications"`
                //Name TwoD `json:"name"`
                Owners []string `json:"owners"`
                Users []string `json:"users"`
               } `json:"bu"`
               } `json:"server"`
    } `json:"devices"`
}

everything is printed as

{[{{{ [SomeOwner1 SomeOwner2] [SomeUser1 SomeUser2]}}}]}

Could someone help me identify what is wrong here?

Here is the link to the golang playground with the struct and sample json. The two TwoD slices are commented in the struct there.

Note:: Edited the playground link with one 2d slice uncommented so that the difference can be noted, and changed the type string to bool, as pointed out by @cnicutar, Thank you.

  • 写回答

1条回答 默认 最新

  • doujuanxun7167 2016-05-28 13:06
    关注

    The main problem is that you're not handling the error returned by json.Unmarshal. Once you handle that, problems with your json (and the decoding struct) become obvious.

    if err := json.Unmarshal(jsonfile, &jsonobject); err != nil {
        fmt.Printf("Unmarshal: %v
    ", err)
    }
    

    First:

    Unmarshal json: cannot unmarshal bool into Go value of type string

    So Add_info should be bool. After fixing that and uncommenting Applications:

    Unmarshal json: cannot unmarshal number into Go value of type string

    After changing 12 to "12" and 106 to "106" the result is:

    {[{{{false [[Systems 12] [SomeProject 106]] [SomeUser1 SomeUser2]}}}]}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题