douyanjing0822 2016-05-08 08:51
浏览 91
已采纳

我写了其余的api,但主体在golang中返回nil。 为什么返回“ 0 {<nil> false [] <nil> false 0 <nil> 0}”

func InsertApData(w http.ResponseWriter, r *http.Request) {

    decoder := json.NewDecoder(r.Body)
    fmt.Printf("Request Body : ", decoder)
    var apdata models.ApData
    err := decoder.Decode(&apdata)
    check(b.E(err))
    _, err2 := stmt.InsertApData.Exec(apdata.Mac, apdata.RssiMax, apdata.RssiMin, apdata.LocDefId)
    check(b.E(err2))
}

and data type for db

type ApData struct {
    ID       int    `db:"id"`
    Mac      string `db:"mac"`
    RssiMax  uint64 `db:"rssi_max"`
    RssiMin  uint16 `db:"rssi_min"`
    LocDefId uint64 `db:"loc_def_id"`
}

Why returning

Request Body : %!(EXTRA *json.Decoder=&{0xc820012b00 [] {[] 0 { false [] false 0 0} { false [] false 0 0} false} 0 { false [] false 0 0} 0 []})

also json :

{
"mac":"01:0a:95:9d:68:20",
"rssi_max":-73.50,
"rssi_min":-50.02,
"loc_def_id":1
}
  • 写回答

1条回答 默认 最新

  • dongshi1868 2016-05-08 11:53
    关注

    If you examine the source code for json.Decoder,

    ...
    type Decoder struct {
        r     io.Reader
        buf   []byte
        d     decodeState
        scanp int // start of unread data in buf
        scan  scanner
        err   error
    
        tokenState int
        tokenStack []int
    }
    
    func NewDecoder(r io.Reader) *Decoder {
        return &Decoder{r: r}
    }
    ...
    

    you can see that NewDecoder only sets the r field, and that is what you are observing in the output of fmt.Printf.

    Until you execute err := decoder.Decode(&apdata), your structure will not be filled in. What you are seeing in the output is the content of the decoder, not the body or your structure.

    If you print your structure after running err := decoder.Decode(&apdata), it contains proper data:

    package main
    
    import (
        "fmt"
        "encoding/json"
        "strings"
    )
    
    func main() {
        input := `{
            "mac":"01:0a:95:9d:68:20",
            "rssi_max":-73.50,
            "rssi_min":-50.02,
            "loc_def_id":1
        }`
    
        decoder := json.NewDecoder(strings.NewReader(input))
        var apdata struct {
                ID       int    `db:"id"`
                Mac      string `json:"mac" db:"mac"`
                RssiMax  uint64 `json:"rssi_max" db:"rssi_max"`
                RssiMin  uint16 `json:"rssi_min" db:"rssi_min"`
                LocDefId uint64 `json:"loc_def_id" db:"loc_def_id"`
        }
        _ = decoder.Decode(&apdata)
        fmt.Printf("appdata : %+v
    ", apdata)
    }
    

    Output:

    appdata : {ID:0 Mac:01:0a:95:9d:68:20 RssiMax:0 RssiMin:0 LocDefId:1}
    

    The RssiMax and RssiMin are zero because they are unsigned ints receiving negative values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥15 C++数组中找第二小的数字程序纠错