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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從 GO 中的 json 數(shù)組映射輸出

從 GO 中的 json 數(shù)組映射輸出

Go
慕碼人8056858 2022-07-11 15:41:12
我需要從 Json 數(shù)組創(chuàng)建一個(gè)地圖,我從下面的 GO 開(kāi)始,有點(diǎn)卡住,有什么指針嗎?    package mainimport (    "encoding/json"    "fmt"    "strconv")func main() {    jsonStr := `{  "employee": [    {      "id": 14325,      "grpname": "senior"    },    {      "id": 234,      "grpname": "junior"    }  ]}`    type Group struct {        Employee []struct {            GroupName string            GroupId   int        }    }    var group []Group    var groupMap []map[string]interface{}    err := json.Unmarshal([]byte(jsonStr), &groupMap)    if err != nil {        panic(err)    }    for _, groupData := range groupMap {        // convert map to array of Group struct        var g Group        g.GroupName = fmt.Sprintf("%s", groupData["grpname"])        g.GroupId, _ = strconv.Atoi(fmt.Sprintf("%v", groupData["id"]))        group = append(group, g)    }    fmt.Println(group)}錯(cuò)誤:./prog.go:45:4: g.GroupName undefined (type Group has no field or method GroupName)./prog.go:46:4: g.GroupId undefined (type Group has no field or method GroupId)預(yù)期輸出:{"senior": 14325,"junior": 234}嘗試了一些類似下面的操作,但出現(xiàn)錯(cuò)誤:出現(xiàn)錯(cuò)誤:%!(EXTRA string=json: cannot unmarshal array into Go struct field GetEmpResponse.employee of type map[string][]model.Employee)類型 GetEmpResponse 結(jié)構(gòu) { Employee map[string][]Employee json:"employee" }為我的測(cè)試嘗試簡(jiǎn)化Json,請(qǐng)參考 play.golang.org
查看完整描述

1 回答

?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

empResponse.Employee是一個(gè)數(shù)組,所以你必須通過(guò)索引訪問(wèn)它的元素,例如

empResponse.Employee[0].ID


查看完整回答
反對(duì) 回復(fù) 2022-07-11
  • 1 回答
  • 0 關(guān)注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)