亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JSON編組接口字節切片時得到奇怪的結果

JSON編組接口字節切片時得到奇怪的結果

Go
臨摹微笑 2022-01-10 16:26:11
我正在使用 Redigo Redis 庫并嘗試 json.Marshal 我從排序集中獲得的結果,但我得到如下結果:"eyJmcm9tSWQiOjEsInRvSWQiOjUsInR5cGUiOjMsInBvc3RJZCI6MSwiY29tbWVudElkIjo0NCwiY3JlYXRlZFRpbWUiOjE0NjMxNTY0ODQsImlzVmlld2VkIjpmYWxzZSwidXNlcm5hbWUiOiJBZG1pbiIsImltYWdlIjoiaHc2ZE5EQlQtMzZ4MzYuanBnIn0="當我應該得到這個時:"{"fromId":5,"toId":1,"type":3,"postId":4,"commentId":49,"createdTime":1463161736,"isViewed":false,"username":"Alexander","image":"JZIfHp8i-36x36.png"}"我有一個通知結構type Notification struct {    FromId int64 `json:"fromId"`    ToId   int64 `json:"toId"`    OfType int64 `json:"type"`    PostId       int64  `json:"postId"`    CommentId    int64  `json:"commentId"`    CreatedTime  int64  `json:"createdTime"`    IsViewed     bool   `json:"isViewed"`    FromUsername string `json:"username"`    FromImage    string `json:"image"`}func New() *Notification {    return &Notification{        CreatedTime: time.Now().Unix(),    }}它有一個方法可以將一串 json 保存到 Redis 排序集中。func (n *Notification) Create(pool *redis.Pool, multicast chan<- []byte) error {    var err error    n.FromUsername, err = validation.FilterUsername(n.FromUsername)    if err != nil {        return err    }    // We can use the same validation as for a username here.    n.FromImage, err = validation.FilterUsername(n.FromImage)    if err != nil {        return err    }}這一切都很好。但后來我想獲取這些結果,然后作為 json 格式的字符串數組發送到客戶端。我保存在排序集中的相同 json 格式的字符串。我正在做這樣簡單的事情。func ByUserId(userId int64, pool *redis.Pool) (interface{}, error) {    key := fmt.Sprintf("user:%d:notification", userId)    c := pool.Get()    defer c.Close()    c.Send("ZREVRANGE", key, 0, -1)    c.Flush()    return c.Receive()}但它不起作用。我能做什么?
查看完整描述

1 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

函數ByUserId可以寫成:


func ByUserId(userId int64, pool *redis.Pool) ([]string, error) {

  key := fmt.Sprintf("user:%d:notification", userId)

  c := pool.Get()

  defer c.Close()

  return redis.Strings(c.Do("ZREVRANGE", key, 0, -1))

}


查看完整回答
反對 回復 2022-01-10
  • 1 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號