如何存取嵌套結構元素
php小編西瓜將為大家介紹如何存取嵌套結構元素。在程式設計中,嵌套結構常用來表示複雜的資料結構,如多層數組或物件。要存取嵌套結構中的元素,我們需要使用適當的語法和方法。本文將詳細解釋如何依照層級逐步存取和取得嵌套結構中的元素,幫助讀者更好地理解和處理嵌套結構資料。無論您是初學者還是有一定經驗的開發者,本文都將為您提供實用的技巧和範例,幫助您輕鬆應對存取嵌套結構元素的需求。
問題內容
我有這樣的結構。我想將我的 json 解析為這個結構。但我無法存取嵌套結構。
我希望能夠達到類似的子結構,但我不能:
func main() { str := `[{ "applicationdefaults": { "applicationpoolname": "defaultapppool", .... }]` mdl := foo(str) // mdl.applicationdefaults ?? i can't reach like this. there are only a few functions like: append!, last! , print!, range!, reverse!, sort!, var! }
有人幫忙嗎?
我的結構:
package model type sitesdetails []struct { applicationdefaults struct { applicationpoolname string `json:"applicationpoolname"` enabledprotocols string `json:"enabledprotocols"` attributes string `json:"attributes"` childelements string `json:"childelements"` elementtagname string `json:"elementtagname"` islocallystored bool `json:"islocallystored"` methods interface{} `json:"methods"` rawattributes string `json:"rawattributes"` schema string `json:"schema"` } `json:"applicationdefaults"` applications []string `json:"applications"` bindings []string `json:"bindings"` id int `json:"id"` limits struct { connectiontimeout string `json:"connectiontimeout"` maxbandwidth int64 `json:"maxbandwidth"` maxconnections int64 `json:"maxconnections"` maxurlsegments int `json:"maxurlsegments"` attributes string `json:"attributes"` childelements string `json:"childelements"` elementtagname string `json:"elementtagname"` islocallystored bool `json:"islocallystored"` methods interface{} `json:"methods"` rawattributes string `json:"rawattributes"` schema string `json:"schema"` } `json:"limits"` }
這是我用來將 json 解析為結構的程式碼:
func foo(resp string) model.SitesDetails { data := []byte(resp) var m model.SitesDetails err := json.Unmarshal(data, &m) if err != nil { log.Fatal(err) } return m }
解決方法
您在這裡解組到一個切片(因為您的sitesdetails
類型是[]struct
並且您的json 以數組開頭),所以您應該能夠透過存取您的詳細資料
poolName := model[0].ApplicationDefaults.ApplicationPoolName
這也解釋了為什麼您的 ide 只建議可應用於切片的操作,例如附加(我猜這會插入正確的程式碼以附加到您的切片)。
順便說一句,你真的不應該調用你的變數model
,因為這也是你顯然用於你的套件的名稱(你正在使用model.sitesdetails
) ,所以你的變數名稱會在此時隱藏該套件- 這可能會導致極大的混亂,任何像樣的ide 都應該警告您這一點。
以上是如何存取嵌套結構元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

OpenSSL,作為廣泛應用於安全通信的開源庫,提供了加密算法、密鑰和證書管理等功能。然而,其歷史版本中存在一些已知安全漏洞,其中一些危害極大。本文將重點介紹Debian系統中OpenSSL的常見漏洞及應對措施。 DebianOpenSSL已知漏洞:OpenSSL曾出現過多個嚴重漏洞,例如:心臟出血漏洞(CVE-2014-0160):該漏洞影響OpenSSL1.0.1至1.0.1f以及1.0.2至1.0.2beta版本。攻擊者可利用此漏洞未經授權讀取服務器上的敏感信息,包括加密密鑰等。

後端學習路徑:從前端轉型到後端的探索之旅作為一名從前端開發轉型的後端初學者,你已經有了nodejs的基礎,...

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

在BeegoORM框架下,如何指定模型關聯的數據庫?許多Beego項目需要同時操作多個數據庫。當使用Beego...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...
