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.

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮