【发布时间】: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。