简体   繁体   中英

REST API + MongoDb ISODate

My GoLang struct:

type myPojo struct {
    ID                bson.ObjectId                 `json:"id" bson:"_id,omitempty"`
    Start             time.Time                     `json:"start"`
}

POST API JSON input request:

{
    "Start":ISODate("2013-10-01T00:00:00.000Z")
}

My code to convert input JSON request to Golang Struct:

func myPostApi(w http.ResponseWriter, r *http.Request, db mongoDB) {
    w.Header().Set("Content-Type", "application/json")
    decoder := json.NewDecoder(r.Body)
    var inputObj myPojo
    err := decoder.Decode(&inputObj)

    if err != nil {
        //This gets executed
        log.Println("Error occurred converting POST input json to myPojo data.")
        log.Println(err)
    }
}

Above code is not able to convert, and it goes inside error if block and prints below, please help.

2018/02/25 22:12:44 Error occurred converting POST input json to myPojo data.
2018/02/25 22:12:44 invalid character 'I' looking for beginning of value

The

ISODate("2013...")

value is not valid JSON. That looks like a symbol or function call, neither of which are allowed in JSON. And there is no date type in JSON:

The "right" JSON date format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM