Go的JSON解码器在最简单的输入上无法工作。为什么?

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

Go's json decoder isn't working on the simplest possible input. Why?

问题

我正在准备用Go编写一个AJAX类型的应用程序,这是一个示例应用程序,以便熟悉它的工作原理。但是它没有。在解码后,InputRec(irec)字段只有零值。首先是源代码:

<!-- language: go -->

package main

import (
  "net/http"
  "encoding/json"
  "fmt"
  // "io/ioutil"
)

type InputRec struct {
  a, b float64
}

type RetRec struct {
  sum float64
}

func addHandler(w http.ResponseWriter, r *http.Request) {
  var outJson []byte
  var irec InputRec
  var orec RetRec
  
/*  inJson, err := ioutil.ReadAll(r.Body)
  num := len(inJson)
  if err != nil {
    panic("Error on reading body")
  }
  r.Body.Close()
  err = json.Unmarshal(inJson, &irec)
  fmt.Println("Input ", num, " bytes: ", string(inJson)) */

  decoder := json.NewDecoder(r.Body)
  err := decoder.Decode(&irec)
  if err != nil {
    panic("Error on JSON decode")
  }
  
  orec.sum = irec.a + irec.b
  fmt.Println("a: ", irec.a, " b: ", irec.b, " Sum: ", orec.sum)
  outJson, err = json.Marshal(orec)
  if err != nil {
    panic("Error on JSON encode")
  }
  
  w.Header().Set("Content-Type", "application/json")
  _, err = w.Write(outJson)
  if err != nil {
    panic("Error writing response")
  }
}

func main() {
  http.HandleFunc("/", addHandler)
  http.ListenAndServe(":1234", nil)
}

现在是测试:

curl -X POST -i -d '{ "a":5.4,"b":8.7}'  http://localhost:1234/
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 2
Date: Sun, 23 Jun 2013 17:01:08 GMT

{}

请注意,我知道请求体已经传递到函数中,因为我尝试过使用注释掉的代码而不是较短的json.Decoder行,它按预期打印了请求体。

在进行上述curl请求时,Println命令的输出如下:

a: 0 b: 0 Sum: 0

很明显,json字段与InputRec(只有a和b)对应,那么这里出了什么问题?

非常感谢!

英文:

I'm preparing to write an AJAX type app in Go and this is a sample app to get familiar with how it would work. But it doesn't. The InputRec (irec) fields just have zeroes after the decode. First the source:

<!-- language: go -->

package main

import (
  &quot;net/http&quot;
  &quot;encoding/json&quot;
  &quot;fmt&quot;
  // &quot;io/ioutil&quot;
)

type InputRec struct {
  a, b float64
}

type RetRec struct {
  sum float64
}

func addHandler(w http.ResponseWriter, r *http.Request) {
  var outJson []byte
  var irec InputRec
  var orec RetRec
  
/*  inJson, err := ioutil.ReadAll(r.Body)
  num := len(inJson)
  if err != nil {
    panic(&quot;Error on reading body&quot;)
  }
  r.Body.Close()
  err = json.Unmarshal(inJson, &amp;irec)
  fmt.Println(&quot;Input &quot;, num, &quot; bytes: &quot;, string(inJson)) */

  decoder := json.NewDecoder(r.Body)
  err := decoder.Decode(&amp;irec)
  if err != nil {
    panic(&quot;Error on JSON decode&quot;)
  }
  
  orec.sum = irec.a + irec.b
  fmt.Println(&quot;a: &quot;, irec.a, &quot; b: &quot;, irec.b, &quot; Sum: &quot;, orec.sum)
  outJson, err = json.Marshal(orec)
  if err != nil {
    panic(&quot;Error on JSON encode&quot;)
  }
  
  w.Header().Set(&quot;Content-Type&quot;, &quot;application/json&quot;)
  _, err = w.Write(outJson)
  if err != nil {
    panic(&quot;Error writing response&quot;)
  }
}

func main() {
  http.HandleFunc(&quot;/&quot;, addHandler)
  http.ListenAndServe(&quot;:1234&quot;, nil)
}

Now the test:

curl -X POST -i -d &#39;{&quot;a&quot;:5.4,&quot;b&quot;:8.7}&#39;  http://localhost:1234/
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 2
Date: Sun, 23 Jun 2013 17:01:08 GMT

{}

Note that I know the request body is making it to the function, because I have tried it with the commented out code instead of shorter json.Decoder lines, and it printed the request body as expected.

When making said curl request, this appears as the output from the Println command:

a: 0 b: 0 Sum: 0

Seems pretty clear that the json fields line up to InputRec (just a and b) so what is wrong here?

Thanks much!

答案1

得分: 2

我明白了。我的结构体成员必须大写。 :/

英文:

I got it. The members of my structs had to be capitalized. :/

huangapple
  • 本文由 发表于 2013年6月24日 01:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/17263144.html
匿名

发表评论

匿名网友

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

确定