使用自引用結構元素解組JSON
考慮以下具有巢狀結構的JSON 字串:
[{ "db": { "url": "mongodb://localhost", "port": "27000", "uname": "", "pass": "", "authdb": "", "replicas": [ { "rs01": { "url":"mongodb://localhost", "port": "27001", "uname": "", "pass": "", "authdb": "" } }, { "rs02": { "url":"mongodb://localhost", "port": "27002", "uname": "", "pass": "", "authdb": "" } } ] } }]
為了在Go 結構中表示此JSON,我們定義以下內容struct:
type DBS struct { URL string `json:"url"` Port string `json:"port"` Uname string `json:"uname"` Pass string `json:"pass"` Authdb string `json:"authdb"` Replicas []DBS `json:"replicas"` }
但是,嘗試將 JSON 字串解組到該結構的切片中會導致空切片。
問題在於 JSON 輸入不是一個事實DBS 結構的切片。相反,它包含另一個 JSON 物件包裝器,並且 DBS 的值屬於「db」屬性。此外,「replicas」屬性包含一個 JSON 數組,其中包含持有不同鍵的對象,這些物件也可以由 DBS 結構體表示。
要完全建模此 JSON,我們需要一個動態類型,例如映射。我們需要的型別是[]map[string]DBS,它代表地圖的切片。映射鍵可以建模任何屬性名稱,值是由 DBS 結構體建模的 JSON 物件。
以下範例示範如何使用此類型完全解析JSON 輸入:
import ( "encoding/json" "fmt" ) type DBS struct { URL string `json:"url"` Port string `json:"port"` Uname string `json:"uname"` Pass string `json:"pass"` Authdb string `json:"authdb"` Replicas []map[string]DBS `json:"replicas"` } func main() { var dbs []map[string]DBS if err := json.Unmarshal([]byte(src), &dbs); err != nil { panic(err) } fmt.Printf("%+v", dbs) }
輸出:
[map[db:{URL:mongodb://localhost Port:27000 Uname: Pass: Authdb: Replicas:[map[rs01:{URL:mongodb://localhost Port:27001 Uname: Pass: Authdb: Replicas:[]}] map[rs02:{URL:mongodb://localhost Port:27002 Uname: Pass: Authdb: Replicas:[]}]]}]]
請注意,第一層總是“db”,可以進一步建模,我們可以切換為指標提高可讀性。
透過理解 JSON 輸入的巢狀和結構,我們可以有效地將其解組為準確表示資料的自訂結構。
以上是如何在 Go 中解組複雜的自引用 JSON?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

goroutinesarefunctionsormethodsthatruncurranceingo,啟用效率和燈威量。 1)shememanagedbodo'sruntimemultimusingmultiplexing,允許千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函數可以從panic中恢復。具體方法是:1)在defer函數中使用recover()捕獲panic,避免程序崩潰;2)記錄詳細的錯誤信息以便調試;3)根據具體情況決定是否恢復程序執行;4)謹慎使用,以免影響性能。

本文討論了使用GO的“字符串”軟件包進行字符串操作,詳細介紹了共同的功能和最佳實踐,以提高效率並有效地處理Unicode。

本文詳細介紹了GO的“時間”包用於處理日期,時間和時區,包括獲得當前時間,創建特定時間,解析字符串以及測量經過的時間。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

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