在golang中使用嵌套结构的json对象

问题描述 投票:0回答:1

我有以下json对象,我试图用Go作为type JsonObject struct表示并将其传回原来的json中,以便我可以将json作为api端点返回。有什么建议/例子吗?

[{
    "time": 173000,
    "id": "VLSuEE5m1kmIhgE7ZhHDFe",
    "height": "",
    "DATASTRUCTURE": {

    },
    "language": "en",
    "size": 0,
    "url": "http://www.gstatic.com/play.m3u8",
    "type": "vid",
    "definitionid": "h264",
    "reference": "PAN-EN",
    "content": "This is some content",
    "revisiondate": "2017-11-29T00:00:00",
    "data": {

    },
    "id": "BBBB3424-153E-49DE-4786-013B6611BBBB",
    "thumbs": {
        "32": "https://www.gstatic.com/images?q=tbn:ANd9GcRj",
        "64": "https://www.gstatic.com/images?q=tbn:DPd3GcRj"
    },
    "title": "Cafeteria",
    "hash": "BBBB5d39bea20edf76c94133be61BBBB"
}]
json go
1个回答
1
投票

您可以使用https://mholt.github.io/json-to-go/为给定的json模式生成结构。例如,问题中给出的json可以表示为:

type AutoGenerated []struct {
Time          int    `json:"time"`
ID            string `json:"id"`
Height        string `json:"height"`
DATASTRUCTURE struct {
} `json:"DATASTRUCTURE"`
Language     string `json:"language"`
Size         int    `json:"size"`
URL          string `json:"url"`
Type         string `json:"type"`
Definitionid string `json:"definitionid"`
Reference    string `json:"reference"`
Content      string `json:"content"`
Revisiondate string `json:"revisiondate"`
Data         struct {
} `json:"data"`
Thumbs struct {
    Num32 string `json:"32"`
    Num64 string `json:"64"`
} `json:"thumbs"`
Title string `json:"title"`
Hash  string `json:"hash"`}

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.