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

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

如何將我的 2d 字符串片段轉換為 []byte?

如何將我的 2d 字符串片段轉換為 []byte?

Go
大話西游666 2023-06-01 18:03:39
我創建了一個 2d 切片并從后端數據庫填充它,但是由于 json.Unmarshal 僅接受 []byte 作為第一個參數,我如何將我的 2d 切片類型轉換為 []byte。這是供參考的示例代碼,因為我無法共享內部代碼:package mainimport (    "encoding/json"    "fmt"    "io/ioutil"    "net/http")//User sjdtype User struct {    EmailList [][]string `json:"emailList"`}func listHandler(w http.ResponseWriter, r *http.Request) {    reqBody, _ := ioutil.ReadAll(r.Body)    var user User    json.Unmarshal(reqBody, &user)    el := user.EmailList     keys := make([][]string, 0)    json.Unmarshal([]byte(el), &keys) //this line not working because []byte(el) not possible    fmt.Println(keys)    w.Header().Set("Content-Type", "application/json")    w.WriteHeader(http.StatusOK)    w.Write([]byte(el))//this line not working because []byte(el) not possible}func main() {    http.HandleFunc("/", listHandler)    http.ListenAndServe(":8080", nil)}樣品要求:{    "emailList": [        [            "[email protected]",            "[email protected]"        ],        [            "[email protected]",            "[email protected]"        ]    ]}
查看完整描述

1 回答

?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

UnmarshalJSON[]byte作為user. Marshal user.EmailList以 JSON 作為[]byte. 例如,


package main


import (

    "encoding/json"

    "fmt"

    "os"

)


type User struct {

    EmailList [][]string `json:"emailList"`

}


func main() {

    // rBody, err := ioutil.ReadAll(r.Body)

    rBody := []byte(`

{

    "emailList": [

        [

            "[email protected]",

            "[email protected]"

        ],

        [

            "[email protected]",

            "[email protected]"

        ]

    ]

}

    `)

    fmt.Println(string(rBody))


    var user User

    err := json.Unmarshal(rBody, &user)

    if err != nil {

        fmt.Fprintln(os.Stderr, err)

        return

    }

    fmt.Println(user.EmailList)


    wData, err := json.Marshal(user.EmailList) // wData is []byte

    if err != nil {

        fmt.Fprintln(os.Stderr, err)

        return

    }

    fmt.Println(wData) // wData is []byte

    fmt.Println(string(wData))

    // _, err = w.Write(wData)

}

游樂場:https://play.golang.org/p/S3HWiTqz--B

輸出:


{

    "emailList": [

        [

            "[email protected]",

            "[email protected]"

        ],

        [

            "[email protected]",

            "[email protected]"

        ]

    ]

}


[[[email protected] [email protected]] [[email protected] [email protected]]]

[91 91 34 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 44 34 98 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 93 44 91 34 108 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 44 34 106 97 107 107 105 64 103 109 97 105 108 46 99 111 109 34 93 93]

[["[email protected]","[email protected]"],["[email protected]","[email protected]"]]



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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