第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在 Go 中從卡夫卡 REST 代理中提取數(shù)據(jù)

在 Go 中從卡夫卡 REST 代理中提取數(shù)據(jù)

Go
智慧大石 2022-10-04 16:48:32
我正在使用卡夫卡的 REST 代理實例來生成和使用消息。使用API獲取新消息,但我無法將這些消息轉換為Go中的結構模型。例如:// Get recordsreq, err := http.NewRequest(http.MethodGet, fmt.Sprintf(FETCH_CONSUMER, URL, GROUP, CONSUMER), nil)if err != nil {    panic(err)}req.Header.Add("Accept", CONTENT_TYPE)respRecords, err := client.Do(req)if err != nil {    panic(err)}defer respRecords.Body.Close()fmt.Printf("Response %s\n", respRecords.Status)fmt.Println(respRecords.Body)recordsBodyResp := bufio.NewScanner(respRecords.Body)for recordsBodyResp.Scan() {    fmt.Printf("<--Body %s\n", recordsBodyResp.Text())    }返回的值采用以下格式:[{"topic":"backward","key":null,"value":{"AdoptionID":"abcd123","IPAddress":"8.8.8.8","Port":"80","Status":"requested"},"partition":0,"offset":7}]由于它是一個對象數(shù)組,因此我想將鍵“value”的值部分提取到結構中。這就是我陷入困境的地方。
查看完整描述

1 回答

?
哆啦的時光機

TA貢獻1779條經(jīng)驗 獲得超6個贊

您可以創(chuàng)建如下結構:


type AutoGenerated []struct {

    Topic     string      `json:"topic"`

    Key       interface{} `json:"key"`

    Value     Value       `json:"value"`

    Partition int         `json:"partition"`

    Offset    int         `json:"offset"`

}

type Value struct {

    AdoptionID string `json:"AdoptionID"`

    IPAddress  string `json:"IPAddress"`

    Port       string `json:"Port"`

    Status     string `json:"Status"`

}


而昂馬歇爾在這種結構中。


請參閱此示例代碼:


package main


import (

    "fmt"

        "encoding/json"


)


func main() {




type Value struct {

    AdoptionID string `json:"AdoptionID"`

    IPAddress  string `json:"IPAddress"`

    Port       string `json:"Port"`

    Status     string `json:"Status"`

}


type AutoGenerated []struct {

    Topic     string      `json:"topic"`

    Key       interface{} `json:"key"`

    Value     Value       `json:"value"`

    Partition int         `json:"partition"`

    Offset    int         `json:"offset"`

}



    byt := []byte(`[{"topic":"backward","key":null,"value":{"AdoptionID":"abcd123","IPAddress":"8.8.8.8","Port":"80","Status":"requested"},"partition":0,"offset":7}]`)

   var dat AutoGenerated


   if err := json.Unmarshal(byt, &dat); err != nil {

        panic(err)

    }

    fmt.Printf("%#v", dat)


}


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號