简体   繁体   中英

Adding outer tag to resulting JSON when marshalling a slice of structs

I struggle with adding an outer tag to my marshaled JSON struct. Here is my example:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "time"
)

type tObj struct {
    ModTime time.Time `json:"mTime"`
    Name    string    `json:"mName"`
}

func main() {
    var objs []tObj
    objs = append(objs, getItem("first"))
    objs = append(objs, getItem("second"))

    json, err := json.MarshalIndent(objs, "", "    ")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(json))
}

func getItem(name string) tObj {
    var t tObj
    t.ModTime = time.Now()
    t.Name = name
    return t
}

So far so good, this gives me something like this:

[{
        "mTime": "2020-06-26T21:29:52.148663+02:00",
        "mName": "first"
    },
    {
        "mTime": "2020-06-26T21:29:52.148666+02:00",
        "mName": "second"
    }
]

All i want is to add an outer tag to the resulting JSON, like this:

{
    "objects": [{
            "mTime": "2020-06-26T21:29:52.148663+02:00",
            "mName": "first"
        },
        {
            "mTime": "2020-06-26T21:29:52.148666+02:00",
            "mName": "second"
        }
    ]
}

I tried to add a json annotation directly to the struct type, but that did not work. What is the easiest way to accomplish this?

You are no longer marshaling an array, you are marshaling an object. You can achieve this by:

json, err := json.MarshalIndent(map[string]interface{}{"objects":objs}, "", "    ")

I struggle with adding an outer tag to my marshaled JSON struct. Here is my example:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "time"
)

type tObj struct {
    ModTime time.Time `json:"mTime"`
    Name    string    `json:"mName"`
}

func main() {
    var objs []tObj
    objs = append(objs, getItem("first"))
    objs = append(objs, getItem("second"))

    json, err := json.MarshalIndent(objs, "", "    ")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(json))
}

func getItem(name string) tObj {
    var t tObj
    t.ModTime = time.Now()
    t.Name = name
    return t
}

So far so good, this gives me something like this:

[{
        "mTime": "2020-06-26T21:29:52.148663+02:00",
        "mName": "first"
    },
    {
        "mTime": "2020-06-26T21:29:52.148666+02:00",
        "mName": "second"
    }
]

All i want is to add an outer tag to the resulting JSON, like this:

{
    "objects": [{
            "mTime": "2020-06-26T21:29:52.148663+02:00",
            "mName": "first"
        },
        {
            "mTime": "2020-06-26T21:29:52.148666+02:00",
            "mName": "second"
        }
    ]
}

I tried to add a json annotation directly to the struct type, but that did not work. What is the easiest way to accomplish this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM