小编Eri*_*ric的帖子

如何将嵌入结构的结构展平为 json

给定以下结构类型,StructA并且StructB嵌入在CompleteStruct

type StructA struct {
    A int `json:"a_a"`
    B int `json:"a_b"`
    C int `json:"a_c"`
}
type StructB struct {
    A int `json:"b_a"`
    B int `json:"b_b"`
}

type CompleteStruct struct {
    Name string `json:"name"`
    StructA
    StructB
}

Run Code Online (Sandbox Code Playgroud)

s是一个新的结构。

s := CompleteStruct{Name: "Example",
    StructA: StructA{
        A: 1, 
        B: 2, 
        C: 3,
    },
    StructB: StructB{
        A: 4,
        B: 5,
    },
}
Run Code Online (Sandbox Code Playgroud)

如何转化s为下面的json.

type StructA struct {
    A int `json:"a_a"`
    B int `json:"a_b"`
    C int …
Run Code Online (Sandbox Code Playgroud)

struct go

5
推荐指数
1
解决办法
1178
查看次数

标签 统计

go ×1

struct ×1