What will be the structure representation of the following json data in golang?

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

What will be the structure representation of the following json data in golang?

问题

{
"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 Device struct {
    ID                  int               `json:"id,omitempty"`
    TargetIp            string            `json:"targetIp,omitempty"`
    Iops                string            `json:"iops,omitempty"`
    Capacity            int               `json:"capacity,omitempty"`
    AllowedVirtualGuests []VirtualGuest    `json:"allowedVirtualGuests,omitempty"`
}

type VirtualGuest struct {
    Name string `json:"Name,omitempty"`
}

func main() {
    var devices []Device
    devices = append(devices, Device{
        ID:         20081691,
        TargetIp:   "10.1.1.1",
        Iops:       "0.25 IOPS per GB",
        Capacity:   20,
        AllowedVirtualGuests: []VirtualGuest{
            {Name: "akhil1"},
            {Name: "akhil2"},
        },
    })
    b, err := json.Marshal(devices)
    if err != nil {
        fmt.Println("json err:", err)
    }
    fmt.Println(string(b))
}

这是一个修正后的示例,其中包含了正确的结构表示和追加方式。你可以根据需要修改和扩展这个示例来添加和删除设备。

英文:
{
  "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))
}

答案1

得分: 0

你可以使用类似json:"id"的结构标签,尝试使用下面的结构体:

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

这样可以在编码和解码 JSON 数据时,指定字段的名称。

英文:

you can use struct tag like json:"id", try struct below:

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

答案2

得分: 0

以下是翻译好的内容:

// 下面是生成的代码,使用了优秀的 [JSON-to-GO][1] 工具:

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

	// 添加一个 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)
}

[1]: https://mholt.github.io/json-to-go/

希望对你有帮助!

英文:

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

huangapple
  • 本文由 发表于 2017年3月3日 13:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/42571081.html
匿名

发表评论

匿名网友

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

确定