猿问

将两个JSON数组与通用密钥golang合并

如今,我尝试根据一个公共密钥合并两个jon。我在一个公共字段中输入了两个不同的JSON,我想基于公共密钥合并两个JSON的数据。两种JSON之间的一种sql连接。


JSON源自此代码


func Dati_plus(c *gin.Context) {


oracle, err := http.Get("http://XXXX/XXX")

    if err != nil {

            panic(err)

    }

defer oracle.Body.Close()


mysql, err := http.Get("http://XXXX/XXX")

if err != nil {

        panic(err)

}

defer mysql.Body.Close()


oracleJSON, err := ioutil.ReadAll(oracle.Body)

if err != nil {

        panic(err)

}

mysqlJSON, err := ioutil.ReadAll(mysql.Body)

if err != nil {

        panic(err)

}


var oracleOUT map[string]interface{}

var mysqlOUT map[string]interface{}


json.Unmarshal([]byte(oracleJSON), &oracleOUT)

json.Unmarshal([]byte(mysqlJSON), &mysqlOUT)


a := map[string]interface{}{"result":mysqlOUT["result"]}

b := map[string]interface{}{"result":oracleOUT["result"]}

输入中的JSON具有这种形式


{"count":2,"result":[{"DESC":"2","NOMEmy":"PIPPO","COGNOMEmy":"PIPPO"},{"DESC":"7","NOMEmy":"PIPPO","COGNOMEmy":"PIPPO"}]


{"count":2,"result":[{"DESC":"2","COS":"PIPPO","ROS":"PIPPO"},{"DESC":"7","COS":"PIPPO","ROS":"PIPPO"},{"DESC":"60","COS":"PIPPO","ROS":"PIPPO"}]

如果我有两个这样的json函数的结果应该是


{"count":2,"result":[{"DESC":"2","COS":"PIPPO","ROS":"PIPPO","NOMEmy":"PIPPO","COGNOMEmy":"PIPPO"},{"DESC":"7","COS":"PIPPO","ROS":"PIPPO","NOMEmy":"PIPPO","COGNOMEmy":"PIPPO"},{"DESC":"60","COS":"PIPPO","ROS":"PIPPO"}]

如果可以帮助的话,这是我用于在两个单值JSON之间进行合并的函数,但是我无法以正确的方式对其进行修改


    func merge(dst, src map[string]interface{}, depth int) map[string]interface{} {

    if depth > MaxDepth {

        panic("Troppo Lungo")

    }

    for key, srcVal := range src {

        if dstVal, ok := dst[key]; ok {

            srcMap, srcMapOk := mapify(srcVal)

            dstMap, dstMapOk := mapify(dstVal)

            if srcMapOk && dstMapOk {

                srcVal = merge(dstMap, srcMap, depth+1)

            }

        }

        dst[key] = srcVal

    }

    return dst

}


请帮我。谢谢


Qyouu
浏览 326回答 1
1回答
随时随地看视频慕课网APP

相关分类

Go
我要回答