我需要将关键字为json标记名的map[string]interface{}转换为struct
type MyStruct struct {
    Id           string `json:"id"`
    Name         string `json:"name"`
    UserId       string `json:"user_id"`
    CreatedAt    int64  `json:"created_at"`
}map[string]interface{}具有密钥id、name、user_id、created_at。我需要将其转换为struct。
发布于 2018-11-14 05:44:51
如果我理解得很好,您有一张map,并且想要按以下方式填充struct。如果是,首先将其更改为jsonString,然后将其解组为struct
package main
import (
    "encoding/json"
    "fmt"
)
type MyStruct struct {
    Id           string `json:"id"`
    Name         string `json:"name"`
    UserId       string `json:"user_id"`
    CreatedAt    int64  `json:"created_at"`
}
func main() {
    m := make(map[string]interface{})
    m["id"] = "2"
    m["name"] = "jack"
    m["user_id"] = "123"
    m["created_at"] = 5
    fmt.Println(m)
    // convert map to json
    jsonString, _ := json.Marshal(m)
    fmt.Println(string(jsonString))
    // convert json to struct
    s := MyStruct{}
    json.Unmarshal(jsonString, &s)
    fmt.Println(s)
}更新2021-08-23
虽然我明白,这篇文章是有用的。我把一个完整的样例放在我的网站上,请查看here。
发布于 2018-11-14 05:52:28
您可以使用https://github.com/mitchellh/mapstructure来实现这一点。默认情况下,它查找标记mapstructure;因此,如果希望使用TagName标记,将json指定为json非常重要。
package main
import (
    "fmt"
    "github.com/mitchellh/mapstructure"
)
type MyStruct struct {
    Id        string `json:"id"`
    Name      string `json:"name"`
    UserId    string `json:"user_id"`
    CreatedAt int64  `json:"created_at"`
}
func main() {
    input := map[string]interface{} {
        "id": "1",
        "name": "Hello",
        "user_id": "123",
        "created_at": 123,
    }
    var output MyStruct
    cfg := &mapstructure.DecoderConfig{
        Metadata: nil,
        Result:   &output,
        TagName:  "json",
    }
    decoder, _ := mapstructure.NewDecoder(cfg)
    decoder.Decode(input)
    fmt.Printf("%#v\n", output)
    // main.MyStruct{Id:"1", Name:"Hello", UserId:"123", CreatedAt:123}
}发布于 2021-02-17 21:49:33
如果考虑到速度,那么Mapstructure比在类似结构上测试的jsoniter.ConfigFastest慢40%
type Message struct {
    ID        string      `json:"id,omitempty"`
    Type      string      `json:"type"`
    Timestamp uint64      `json:"timestamp,omitempty"`
    Data      interface{} `json:"data"`
}并且您希望通过Type == "myType"检查消息类型,然后仅编组/解组数据字段(在第一次解组后将是mapstringinterface{} )非常类似于您提到的场景,以及https://github.com/mitchellh/mapstructure用于...
goos: windows
goarch: amd64
cpu: AMD Ryzen Threadripper 3960X 24-Core Processor 
BenchmarkMarshalUnmarshal
BenchmarkMarshalUnmarshal-48                      521784          2049 ns/op         871 B/op         20 allocs/op
BenchmarkMarshalUnmarshalConfigFastest
BenchmarkMarshalUnmarshalConfigFastest-48         750022          1591 ns/op         705 B/op         15 allocs/op
BenchmarkMapstructure
BenchmarkMapstructure-48                          480001          2546 ns/op        1225 B/op         25 allocs/op
BenchmarkDirectStruct
BenchmarkDirectStruct-48                         3096033           391.7 ns/op        88 B/op          3 allocs/op
PASShttps://stackoverflow.com/questions/48994008
复制相似问题