Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"invalid character '1' after top-level value " unmarshaling JSON

Tags:

json

go

I am using json to store data on disk between program calls, the program runs fine for some time, but after that it displays error in json decoding, "invalid character '1' after top-level value ".

Can anyone suggest some solution to this problem ?

like image 206
Pankaj Avatar asked Jul 03 '10 09:07

Pankaj


1 Answers

Instead of doing the manual file opening, consider using some of the inbuilt IO functions.

import (
  "io/ioutil"
  "encoding/json"
)
...
func Save(myobj SomeType, filename string) (err error) {
    var data []byte
    if data, err = json.Marshal(myobj); err != nil {
        return
    }
    return ioutil.WriteFile(filename, data)
}

The same goes for loading of json data where you use ioutil.ReadFile and json.Unmarshal.

like image 165
jimt Avatar answered Oct 10 '22 23:10

jimt