How can I fix my json file structure in Golang

  Kiến thức lập trình

I have a problem while writting json data to my json file. I do it in incorrect format and I don’t know how to fix it.

Here’s the incorrect structure:
{"todo":{"done":false,"task":"test3"}} {"todo":{"done":false,"task":"test3"}} {"todo":{"done":false,"task":"test2"}}

Here’s the code for it
`func WriteJSONFile(value string) {

filename := "todobudData.json"

data := map[string]interface{}{
    "todo": map[string]interface{}{
        "done": false,
        "task": value,
    },
}

jsonData, err := json.Marshal(data)
if err != nil {
    fmt.Println("sad boi")
    return
}

fmt.Println(string(jsonData))
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
    log.SetFlags(log.Ldate | log.Lshortfile)
    log.Println(err)
}

f.WriteString(string(jsonData))
f.Close()

}
`

I don’t know how to write json file with specific format. I would like, later to retrive individual tasks form the json file, and I dont have any idea how to fix its format.

I tried to convert the map to the var and to wrap object in json array but it didn’t work

New contributor

leopoldmiszcz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT