我有这个版本的代码:我需要它来更改代码的某些部分或只是帮助我,因为我检查了谷歌中的所有链接。
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"]
}
你能帮我改变我的代码的某些部分来修复它吗?
慕姐4208626
相关分类