gpt4 book ai didi

json - 在同一文件中添加两个json文件

转载 作者:行者123 更新时间:2023-12-01 22:22:25 25 4
gpt4 key购买 nike

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

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

您可以帮助我更改代码的某些部分以对其进行修复吗?

我的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"
}

最佳答案

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)
}

关于json - 在同一文件中添加两个json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62363339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com