Go Lang的RESTful API无法正常工作,JSON格式的数据无法处理。

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

Go Lang RESTful API not working JSON

问题

我正在尝试在Go语言上创建一个返回JSON值的RESTful API。当我加载页面时,页面上没有任何值。有人可以帮我解决这个问题吗?

type sessiond struct{
   apiKey string `json:"apiKey"`
   token string `json:"token"`
}

func dummy(w http.ResponseWriter, r *http.Request) {
   se:=sessiond{apiKey:key,token:"erer"}
   log.Println(se);    // 我在这里得到了值!但页面上没有任何内容。
   w.Header().Set("Content-Type", "application/json; charset=UTF-8")
   w.WriteHeader(http.StatusOK)
      if err := json.NewEncoder(w).Encode(se); err != nil {
      panic(err)
   }
   //res.R200(w, se)
}
英文:

I'm trying to make a RESTful API on Go Lang returning JSON value. I am not getting any value on the page when I load it. Could anyone help me out here.. ?

type sessiond struct{
   apiKey string `json:"apiKey"`
   token string `json:"token"`
}

func dummy(w http.ResponseWriter, r *http.Request) {
   se:=sessiond{apiKey:key,token:"erer"}
   log.Println(se);    // Iam getting the value here ! but nothing on the page.
   w.Header().Set("Content-Type", "application/json; charset=UTF-8")
   w.WriteHeader(http.StatusOK)
      if err := json.NewEncoder(w).Encode(se); err != nil {
      panic(err)
   }
   //res.R200(w, se)
}

答案1

得分: 2

sessiond类型中的字段导出,通过以大写字母开头的字段名来实现。

type sessiond struct{
   ApiKey string `json:"apiKey"`
   Token string `json:"token"`
}

JSON编码器和解码器会忽略未导出的字段。

英文:

Export the fields in type sessiond by starting the field name with an uppercase letter.

type sessiond struct{
   ApiKey string `json:"apiKey"`
   Token string `json:"token"`
}

The JSON encoder and decoder ignore unexported fields.

huangapple
  • 本文由 发表于 2015年8月10日 13:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/31911961.html
匿名

发表评论

匿名网友

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

确定