dongzuan4917 2017-03-03 05:05
浏览 80
已采纳

golang中以下json数据的结构表示形式是什么?

{
  "devices": [
    {
      "id": 20081691,
      "targetIp": "10.1.1.1",
      "iops": "0.25 IOPS per GB",
      "capacity": 20,
      "allowedVirtualGuests": [
        {
          "Name": "akhil1"
        },
        {
          "Name": "akhil2"
        }
      ]
    }
  ]
}

How to write a structure representation of this JSON data so that I can add and delete devices to the list. I tried with the different structure representations but nothing is working. Below is one of the example I have tried with a similar kind of json data. I am not able to add new data to it. The structure representation and the way the append is done might be wrong here

package main

import (
    "encoding/json"
    "fmt"
)

type Person struct {
    ID        string   `json:"id,omitempty"`
    Firstname string   `json:"firstname,omitempty"`
    Lastname  string   `json:"lastname,omitempty"`
    Address   []Address `json:"address,omitempty"`
}

type Address[] struct {
    City  string `json:"city,omitempty"`

}


func main() {

var people []Person
people = append(people, Person{ID: "1", Firstname: "Nic", Lastname: "Raboy", Address: []Address{{City: "Dublin"},{ City: "CA"}}} )
b, err := json.Marshal(people)
    if err != nil {
        fmt.Println("json err:", err)
    }
    fmt.Println(string(b))
}
  • 写回答

2条回答 默认 最新

  • duanping5306 2017-03-03 07:15
    关注

    It will be below. This was generated using the excellent JSON-to-GO tool:

    type MyStruct struct {
        Devices []struct {
            ID                   int    `json:"id"`
            TargetIP             string `json:"targetIp"`
            Iops                 string `json:"iops"`
            Capacity             int    `json:"capacity"`
            AllowedVirtualGuests []struct {
                Name string `json:"Name"`
            }                           `json:"allowedVirtualGuests"`
        }                               `json:"devices"`
    }
    

    To simplify that though, you can break it into smaller structs for readability. An example is below:

    package main
    
    import "fmt"
    
    type VirtualGuest struct {
        Name string `json:"Name"`
    }
    
    type Device struct {
        ID                   int            `json:"id"`
        TargetIP             string         `json:"targetIp"`
        Iops                 string         `json:"iops"`
        Capacity             int            `json:"capacity"`
        AllowedVirtualGuests []VirtualGuest `json:"allowedVirtualGuests"`
    }
    
    type MyStruct struct {
        Devices []Device `json:"devices"`
    }
    
    func main() {
    
        var myStruct MyStruct
    
        // Add a MyStruct
        myStruct.Devices = append(myStruct.Devices, Device{
            ID:1,
            TargetIP:"1.2.3.4",
            Iops:"iops",
            Capacity:1,
            AllowedVirtualGuests:[]VirtualGuest{
                VirtualGuest{
                    Name:"guest 1",
                },
                VirtualGuest{
                    Name:"guest 2",
                },
            },
        })
    
        fmt.Printf("MyStruct: %v
    ", myStruct)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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时遇到的编译问题