json.NewDecoder.Decode()方法在解析整数时出现问题。

huangapple go评论152阅读模式
英文:

json NewDecoder Decode not parsing integers

问题

我正在尝试创建一个API端点,用于将数据保存到数据库中,然而,通过POST请求传递的整数似乎无法正确解析。

这是我的结构体:

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"`
}

这是我将JSON解析为结构体的“有问题”的函数:

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

我使用httprouter作为我的路由器,我发送POST请求的路由定义如下:

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

最后,我发送的测试POST请求具有以下有效载荷:

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

问题出现在我尝试使用jsonForm.Form时,我传递的整数如time_limitscore_to_pass都是0。

{0 0 test 0 0  true true document    []}
英文:

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 like time_limitandscore_to_pass` are 0

{0 0 test 0 0  true true document    []}

答案1

得分: 2

找到了!

需要在我的json结构声明的末尾添加string,不确定为什么,但它起作用了。

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"`
}
英文:

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"`
}

huangapple
  • 本文由 发表于 2017年1月19日 18:04:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/41738937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定