我有一个工作代码:
data, err := ioutil.ReadFile("D:/Go/Go_project/Go_pro/firstfile.json")
if err != nil {
fmt.Print(err)
}
data1, err := ioutil.ReadFile("D:/Go/Go_project/Go_pro/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)
}
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)
}
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)
}
a, err := json.Marshal(migrations)
a2, err := json.Marshal(migrations1)
if err != nil {
fmt.Println(err)
}
os.Stdout.Write(a)
os.Stdout.Write(a2)
最后我得到:
{"data":["is nice","is a good person"],"date":["2012-04-23T18:24:59.511Z","2012-04-23T18:25:00.511Z"],"name":["Kate","Gleison"]}
我需要如何更改代码,最终将得到:
{
"name":["kate","gleison"],
"date":"2012-04-23T18:00:00.000Z",
"data":["is nice","is a good person"]
}
这是我的 2 个 json 文件。
第一的:
{
"name":"Kate",
"date":"2012-04-23T18:24:59.511Z",
"data":"is nice"
}
第二:
{
"name":"Gleison",
"date":"2012-04-23T18:25:00.511Z",
"data":"is a good person"
}
慕桂英546537
相关分类