首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用json标记构造的map[string]接口{}

使用json标记构造的map[string]接口{}
EN

Stack Overflow用户
提问于 2018-02-27 01:33:19
回答 3查看 26.5K关注 0票数 17

我需要将关键字为json标记名的map[string]interface{}转换为struct

代码语言:javascript
运行
复制
type MyStruct struct {
    Id           string `json:"id"`
    Name         string `json:"name"`
    UserId       string `json:"user_id"`
    CreatedAt    int64  `json:"created_at"`
}

map[string]interface{}具有密钥idnameuser_idcreated_at。我需要将其转换为struct

EN

回答 3

Stack Overflow用户

发布于 2018-11-14 05:44:51

如果我理解得很好,您有一张map,并且想要按以下方式填充struct。如果是,首先将其更改为jsonString,然后将其解组为struct

代码语言:javascript
运行
复制
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

票数 22
EN

Stack Overflow用户

发布于 2018-11-14 05:52:28

您可以使用https://github.com/mitchellh/mapstructure来实现这一点。默认情况下,它查找标记mapstructure;因此,如果希望使用TagName标记,将json指定为json非常重要。

代码语言:javascript
运行
复制
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}
}
票数 15
EN

Stack Overflow用户

发布于 2021-02-17 21:49:33

如果考虑到速度,那么Mapstructure比在类似结构上测试的jsoniter.ConfigFastest慢40%

代码语言:javascript
运行
复制
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用于...

代码语言:javascript
运行
复制
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
PASS
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48994008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档