donglanying3855 2017-01-19 10:04
浏览 161
已采纳

json NewDecoder解码不解析整数

I'm trying to make an API endpoint which would save stuff into a database however, the integers which I pass in via POST request don't seem to be parsing correctly

Here's my structs:

type OnlineTestForm struct {
  Form  OnlineTestSet   `json:"form"`
}
type OnlineTestSet struct {
  ID                   int      `db:"id"                     json:"id"`
  OnlineTestSubjectId  int      `db:"online_test_subject_id" json:"online_test_subject_id"`
  Name                 string   `db:"name"                   json:"name"`
  ScoreToPass          int      `db:"score_to_pass"          json:"score_to_pass"`
  TimeLimit            int      `db:"time_limit"             json:"time_limit"`
  Description          string   `db:"description"            json:"description"`
  Enabled              bool     `db:"enabled"                json:"enabled"`
  Online               bool     `db:"online"                 json:"online"`
  TestType             string   `db:"test_type"              json:"test_type"`
  DocName              string   `db:"doc_name"               json:"doc_name"`
}

And the "problematic" function where I parse the JSON into a struct:

func NewOnlineTest(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
  var jsonForm OnlineTestForm
  json.NewDecoder(r.Body).Decode(&jsonForm)
  err := addNewOnlineTest(jsonForm.Form)
  if err != nil {
    fmt.Println(err)
    w.WriteHeader(500)
    fmt.Fprint(w, "{\"error\": {\"message\": \"An error occured while adding new test - "+err.Error()+"\"}}")
    return
  }

  w.WriteHeader(200)
}

I'm using httprouter for as my router and the route to which I post is defined like this

router.POST("/onlinetest/new", NewOnlineTest)

And finally the test POST request which I send has the following payload:

{
  "form":{
    "name":"test",
    "test_type":"document",
    "score_to_pass":32,
    "time_limit":324,
    "enabled":true,
    "online":true,
    "online_test_subject_id":1
    }
  }

The problem occurs when I try to use jsonForm.Form' but the integers I've passed in liketime_limitandscore_to_pass` are 0

{0 0 test 0 0  true true document    []}
  • 写回答

1条回答 默认 最新

  • dongnao2582 2017-01-19 10:42
    关注

    Found it!

    Needed to add string at the end of my json struc declaration, not sure why but it worked

    type OnlineTestSet struct {
      ID                   int      `db:"id"                     json:"id"`
      OnlineTestSubjectId  int      `db:"online_test_subject_id" json:"online_test_subject_id,string"`
      Name                 string   `db:"name"                   json:"name"`
      ScoreToPass          int      `db:"score_to_pass"          json:"score_to_pass,string"`
      TimeLimit            int      `db:"time_limit"             json:"time_limit,string"`
      Description          string   `db:"description"            json:"description"`
      Enabled              bool     `db:"enabled"                json:"enabled"`
      Online               bool     `db:"online"                 json:"online"`
      TestType             string   `db:"test_type"              json:"test_type"`
      DocName              string   `db:"doc_name"               json:"doc_name"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable