为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
Qyouu 2021-04-12 17:14:40
如今,我尝试根据一个公共密钥合并两个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}请帮我。谢谢
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 325 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信