登录
首页 >  Golang >  Go问答

合并三个 JSON 在 GO 中

来源:stackoverflow

时间:2024-02-25 17:36:26 265浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《合并三个 JSON 在 GO 中》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

现在,我正在为 rest api 编写 go

现在,我得到了 3 个 json (str_data_table / str_all_col / str_all_domain),如下所示(这些 json 是切片数据类型)

str_data_table

{
    "table" : "tablea",
    "table_type" : "pubilc"
}

str_all_col

[ 
    { 
        "table" : "tablea",
        "field" : "name",
        "type". : "string"
    },
    { 
        "table" : "tablea",
        "field" : "surname"
        "type". : "string"
    },
]

str_all_domain

[
    {
        "field" : "name",
        "domain" : "xxx",
        "element" : "jjjjj"
    },
    {
        "field" : "name",
        "domain" : "yyyy",
        "element" : "ssss"
    },
    {
        "field" : "surname",
        "domain" : "aaaaaa",
        "element" : "dddd"
    },
    {
        "field" : "surname",
        "domain" : "bbbbbb",
        "element" : "eeee"
    }
]

我需要将这 3 个 json 分组为一个 json,如下所示

{
    "table" : "tablea",
    "table_type" : "pubilc"
    "field" : [
        {
            "field" : "name",
            "type". : "string",
            "detail" : [
                {
                    "domain" : "xxx",
                    "element" : "jjjjj"
                },
                {
                    "domain" : "yyyy",
                    "element" : "ssss"
                }
            ]
        },
        {
            "field" : "surname",
            "type". : "string",
            "detail" : [ 
                {
                    "domain" : "aaaaaa",
                    "element" : "dddd"
                },
                {
                    "domain" : "bbbbbb",
                    "element" : "eeee"
                }
            ]
        }
    ]
}

所以我使用 sqlc / chi-router / postgresql 在 go 中编写代码

type str_domain_into struct {
        domain  string
        element string
    }

    type str_column_info struct {
        field  string
        type   string
        domain []str_domain_into
    }

    type str_table struct {
        table     string
        tabletype string
        column    []str_column_info
    }

    var mixdata []str_table
    var w_mixdata str_table
    var w_col str_column_info
    var w_domain str_domain_into

    for _, datatable := range str_data_table {

        w_mixdata.table = datatable.table
        w_mixdata.tabletype = datatable.tabletype

        for _, datacolumn := range str_all_col {

            if datatable.table != datacolumn.table {
                continue
            }

            w_col.field = datacolumn.field
            w_col.type = datacolumn.type

            for _, datadomain := range str_all_domain {

                if datacolumn.field != datadomain.field {
                    continue
                }

                w_domain.domain = datadomain.domain
                w_domain.element = datadomain.element

                w_col.domain = append(w_col.domain, w_domain)

            }

            w_mixdata.column = append(w_mixdata.column, w_col)

        }

        mixdata = append(mixdata, w_mixdata)
    }

    respondwithjson(w, 200, mixdata)

但是 mixdata 的响应只有如下两个字段

{
    "table" : "tablea",
    "table_type" : "pubilc"
}

func respondwithjson 的附加代码

func respondwithjson(w http.responsewriter, code int, payload interface{}) {
    // the payload is struct data

    data, err := json.marshal(payload)
    if err != nil {
        log.printf("failed to marshal json response : %v", payload)
        w.writeheader(500)
        return
    }

    w.header().add("content-type", "application/json")
    w.writeheader(200)
    w.write(data)
}

请帮忙。谢谢

预期结果应如下

{
    "table" : "tablea",
    "table_type" : "pubilc"
    "field" : [
        {
            "field" : "name",
            "type". : "string",
            "detail" : [
                {
                    "domain" : "xxx",
                    "element" : "jjjjj"
                },
                {
                    "domain" : "yyyy",
                    "element" : "ssss"
                }
            ]
        },
        {
            "field" : "surname",
            "type". : "string",
            "detail" : [ 
                {
                    "domain" : "aaaaaa",
                    "element" : "dddd"
                },
                {
                    "domain" : "bbbbbb",
                    "element" : "eeee"
                }
            ]
        }
    ]
}

正确答案


谢谢@Trock

type str_column_info struct {
    Field  string
    Type   string
    domain []str_domain_into // Wrong
    Domain []str_domain_into // Correct
}

type str_table struct {
    Table     string
    TableType string
    column    []str_column_info // Wrong
    Column    []str_column_info // Correct
}

本篇关于《合并三个 JSON 在 GO 中》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>