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

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

Golang 解組 json 映射和數組

Golang 解組 json 映射和數組

Go
慕少森 2021-12-20 10:25:07
在閱讀了JSON 和 Go 之后,我確實了解了在 Go 中解碼 json 的基礎知識。但是,存在這個問題,輸入 json 有時可以是地圖,有時是地圖數組。考慮以下問題:package mainimport (    "encoding/json"    "fmt")func main() {    b := []byte(`[{"email":"[email protected]"}]`)    c := []byte(`{"email":"[email protected]"}`)    var m interface{}    json.Unmarshal(b, &m)    switch v := m.(type) {    case []interface{}:        fmt.Println("this is b", v)    default:        fmt.Println("No type found")    }    json.Unmarshal(c, &m)    switch v := m.(type) {    case map[string]interface{}:        fmt.Println("this is c", v)    default:        fmt.Println("No type found")    }}現在,我如何獲得價值email:[email protected]在這兩種情況下(b& c)問題:如果 json 是一個數組,我想遍歷每個并打印電子郵件。如果json是地圖,我想直接打印email播放:http : //play.golang.org/p/UPoFxorqWl
查看完整描述

3 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

根據我的經驗,使用 interface{} 處理 json 解碼可能會導致一些奇怪的問題,我傾向于避免它。盡管有一些方法可以使用反射包來實現它。


這是基于您的原始解決方案的問題解決方案,希望對您有所幫助。


package main


import (

    "encoding/json"

    "fmt"

)


type Item struct {

    Email string `json:email`

}


func main() {

    b := []byte(`[{"email":"[email protected]"}]`)

    //b := []byte(`{"email":"[email protected]"}`)


    var m = &Item{}

    var ms = []*Item{}

    err := json.Unmarshal(b, &m)

    if err != nil {

        err = json.Unmarshal(b, &ms)

        if err != nil {

            panic(err)

        }

        for _, m := range ms {

            fmt.Println(m.Email)

        }

    } else {

        fmt.Println(m.Email)

    }


}


查看完整回答
反對 回復 2021-12-20
?
藍山帝景

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

這是你想要的嗎?http://play.golang.org/p/8yrirlUAnp


package main


import (

    "encoding/json"

    "fmt"

)


func main() {

    b := []byte(`[{"email":"[email protected]"}]`)

    c := []byte(`{"email":"[email protected]"}`)


    var m interface{}


    json.Unmarshal(b, &m)

    switch v := m.(type) {

    case []interface{}:

        for _, x := range v {

            fmt.Println("this is b", x.(map[string]interface{})["email"])

        }

    default:

        fmt.Println("No type found")

    }


    json.Unmarshal(c, &m)

    switch v := m.(type) {

    case map[string]interface{}:

        fmt.Println("this is c", v["email"])

    default:

        fmt.Println("No type found")

    }


}

編輯:錯過了循環部分,添加了它。


查看完整回答
反對 回復 2021-12-20
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

這不是最慣用的方法,但另一種方法是嘗試將其編組為您想要的類型。這里的優點是沒有超出需要的額外反射。


http://play.golang.org/p/dV5qCu3tKk


package main


import (

    "encoding/json"

    "fmt"

)


func main() {

    fmt.Println(extract([]byte(`[{"email":"[email protected]"}]`)))

    fmt.Println(extract([]byte(`{"email":"[email protected]"}`)))

}


func extract(b []byte) string {

    var m map[string]string


    err := json.Unmarshal(b, &m)

    if err == nil {

        return m["email"]

    }


    var nested []map[string]string


    err = json.Unmarshal(b, &nested)

    if err == nil {

        for _, m := range nested {

            return m["email"]

        }

    }

    return ""

}


查看完整回答
反對 回復 2021-12-20
  • 3 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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