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

将两个相同文件的json文件合二为一

将两个相同文件的json文件合二为一

Go
PIPIONE 2022-06-13 10:32:47
我有这个版本的代码:我需要它来更改代码的某些部分或只是帮助我,因为我检查了谷歌中的所有链接。data, err := ioutil.ReadFile("firstfile.json")    if err != nil {        fmt.Print(err)    }    data1, err := ioutil.ReadFile("secondfile.json")    if err != nil {        fmt.Print(err)    }var migrations map[string]interface{}var migrations1 map[string]interface{}err = json.Unmarshal([]byte(data), &migrations)err = json.Unmarshal([]byte(data1), &migrations1)if err != nil {    log.Println("Error:", err)}//fmt.Println(migrations)//fmt.Println(migrations1)mig := migrations["date"]mig1 := migrations1["date"]layout := "2006-01-02T15:04:05.000Z"str := fmt.Sprint(mig)str1 := fmt.Sprint(mig1)t, err := time.Parse(layout, str)t1, err := time.Parse(layout, str1)if err != nil {    fmt.Println(err)}//fmt.Println(t, t1)firsthour := t.Hour()secondhour := t1.Hour()if firsthour == secondhour {    result := []map[string]interface{}{}    migrations3 := make(map[string]interface{})    for k, v := range migrations {        if _, ok := migrations[k]; ok {            migrations3[k] = v        }    }    for k, v := range migrations1 {        if _, ok := migrations1[k]; ok {            migrations3[k] = v        }    }    result = append(result, migrations, migrations1)    fmt.Println(result)    b, err := json.Marshal(result)    if err != nil {        fmt.Println(err)    }    os.Stdout.Write(b)} else {    err = json.Unmarshal([]byte(data), &migrations)    err = json.Unmarshal([]byte(data1), &migrations1)    fmt.Println(migrations, migrations)}}最后我得到了:[{"data":"is nice",  "date":"2012-04-23T18:24:59.511Z",  "name":"Kate"}, {"data":"is a good person",  "date":"2012-04-23T18:25:00.511Z",  "name":"Gleison"}]但我想得到什么:{    "name":["kate","gleison"],    "date":"2012-04-23T18:00:00.000Z",    "data":["is nice","is a good person"]}你能帮我改变我的代码的某些部分来修复它吗?
查看完整描述

1 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

migrations, migrations1 := make(map[string]interface{}), make(map[string]interface{})


data := `{ "name":"Kate", "date":"2012-04-23T18:24:59.511Z", "data":"is nice" }`

data1 := `{ "name":"Gleison", "date":"2012-04-23T18:25:00.511Z", "data":"is a good person" }`


err := json.Unmarshal([]byte(data), &migrations)

if err != nil {

    panic(err)

}


err = json.Unmarshal([]byte(data1), &migrations1)

if err != nil {

    panic(err)

}


layout := "2006-01-02T15:04:05.000Z"


t, err := time.Parse(layout, fmt.Sprint(migrations["date"]))

t1, err := time.Parse(layout, fmt.Sprint(migrations1["date"]))

if err != nil {

    fmt.Println(err)

}


firsthour := t.Hour()

secondhour := t1.Hour()


if firsthour == secondhour {

    result := make(map[string][]interface{})


    for k, v := range migrations {

        result[k] = append(result[k], v)

    }


    for k, v := range migrations1 {

        result[k] = append(result[k], v)

    }


    fmt.Println(result)


    b, err := json.Marshal(result)

    if err != nil {

        fmt.Println(err)

    }


    os.Stdout.Write(b)


} else {

    err = json.Unmarshal([]byte(data), &migrations)

    if err != nil {

        panic(err)

    }


    err = json.Unmarshal([]byte(data1), &migrations1)

    if err != nil {

        panic(err)

    }


    fmt.Println(migrations, migrations)

}


查看完整回答
反对 回复 2022-06-13
  • 1 回答
  • 0 关注
  • 108 浏览
慕课专栏
更多

添加回答

举报

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