【问题标题】:REST API + MongoDb ISODateREST API + MongoDb ISODate
【发布时间】:2018-02-25 21:28:35
【问题描述】:

我的 GoLang 结构:

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

POST API JSON 输入请求:

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

我将输入 JSON 请求转换为 Golang 结构的代码:

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

上面的代码无法转换,如果阻塞并打印在下面,就会进入错误,请帮助。

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

【问题讨论】:

  • 打印出来是什么错误?
  • 您好@Mark,在主要问题中添加了错误“无效字符'我'正在寻找值的开头”,感谢您对此进行调查。
  • 该错误表明请求正文无法解码,因为它不是有效的 json。根据json specification,仔细查看请求主体本身,并检查其是否为有效 json。

标签: mongodb date go isodate


【解决方案1】:

ISODate("2013...")

值不是有效的 JSON。这看起来像一个符号或函数调用,在 JSON 中都不允许。 JSON 中没有日期类型:

The "right" JSON date format

【讨论】:

    猜你喜欢
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 2017-05-21
    相关资源
    最近更新 更多