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

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

Golang:使用字符串作為鍵解組以下 json 的最佳方法

Golang:使用字符串作為鍵解組以下 json 的最佳方法

Go
慕的地8271018 2021-12-27 18:11:26
我有 json 喜歡{  "api_type" : "abc",  "api_name" : "xyz",  "cities" : {    "new_york" : {        "lat":"40.730610",        "long":"-73.935242"    },    "london" : {        "lat":"51.508530",        "long":"-0.076132"    },    "amsterdam" : {        "lat":"52.379189",        "long":"4.899431"    }    //cities can be multiple  }}我可以使用以下結構解組type MyJsonName struct {    APIName   string `json:"api_name"`    APIType   string `json:"api_type"`    Locations struct {        Amsterdam struct {            Lat  string `json:"lat"`            Long string `json:"long"`        } `json:"amsterdam"`        London struct {            Lat  string `json:"lat"`            Long string `json:"long"`        } `json:"london"`        NewYork struct {            Lat  string `json:"lat"`            Long string `json:"long"`        } `json:"new_york"`    } `json:"locations"`}但是我的城市名稱和數字在每個響應中都會不同,解組這種類型的 json 的最佳方法是什么,其中鍵可以是不同的字符串。
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

我會制作locations一張地圖(雖然你cities在 JSON 中調用了它):


type MyJsonName struct {

        APIName   string `json:"api_name"`

        APIType   string `json:"api_type"`

        Locations map[string]struct {

                Lat  string

                Long string

        } `json:"locations"`

}



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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