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

时间:2017-03-03 05:05:53

标签: json go

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

如何编写此JSON数据的结构表示,以便我可以向列表添加和删除设备。我尝试了不同的结构表示,但没有任何工作。下面是我尝试使用类似json数据的示例之一。我无法向其添加新数据。结构表示和附加的方式可能在这里是错误的

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 个答案:

答案 0 :(得分:0)

你可以使用像json:"id"这样的struct标签,试试下面的结构:

type Data struct {
        Devices []struct {
                Id                   int    `json:"id"`
                IP                   string `json:"targetIp"`
                IOPS                 string `json:"iops"`
                Capacity             int    `json:"capacity"`
                AllowedVirtualGuests []struct {
                        Name string `json:"Name"`
                } `json:"allowedVirtualGuests"`
        } `json:"devices"`
}

答案 1 :(得分:0)

它将在下面。这是使用优秀的JSON-to-GO工具生成的:

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"`
}

为了简化这一点,您可以将其分解为更小的结构以提高可读性。一个例子如下:

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\n", myStruct)
}