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

我有这个版本的代码:我需要它来更改代码的某些部分或只是帮助我,因为我检查了谷歌中的所有链接。


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"]

}

你能帮我改变我的代码的某些部分来修复它吗?


PIPIONE
浏览 109回答 1
1回答

慕姐4208626

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)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go