在Go Lang 中循環/迭代第二級嵌套JSON
考慮遇到如下所示的嵌套JSON 結構的場景:
{ "outterJSON": { "innerJSON1": { "value1": 10, "value2": 22, "InnerInnerArray": [ "test1" , "test2"], "InnerInnerJSONArray": [ {"fld1" : "val1"} , {"fld2" : "val2"} ] }, "InnerJSON2":"NoneValue" } }
任務是有效地迭代此結構並將所有鍵值對作為字串檢索以進行進一步處理。不幸的是,為這樣的動態 JSON 輸入手動定義結構是不可行的。
高效迭代方法
為了有效地應對這一挑戰,採用了遞歸方法:
func parseMap(m map[string]interface{}) { for key, val := range m { // Check the type of the value switch concreteVal := val.(type) { case map[string]interface{}: // If it's a nested map, recursively call the function parseMap(val.(map[string]interface{})) case []interface{}: // If it's a nested array, call the function to parse the array parseArray(val.([]interface{})) default: // For all other types, print the key and value as a string fmt.Println(key, ":", concreteVal) } } }
這個遞歸函數 parseMap 檢查映射中每個值的類型。如果該值本身是一個映射,它會遞歸呼叫 parseMap 來遍歷該巢狀映射。如果該值是一個數組,它會呼叫 parseArray 來迭代它。對於所有其他類型(例如字串、數字等),它只是將鍵和值列印為字串。
示範
考慮前面提供的範例 JSON 輸入。運行下面的程式碼將產生以下輸出:
func parseArray(a []interface{}) { for i, val := range a { // Check the type of the value switch concreteVal := val.(type) { case map[string]interface{}: // If it's a nested map, recursively call the function parseMap(val.(map[string]interface{})) case []interface{}: // If it's a nested array, call the function to parse the array parseArray(val.([]interface{})) default: // For all other types, print the index and value as a string fmt.Println("Index:", i, ":", concreteVal) } } } const input = ` { "outterJSON": { "innerJSON1": { "value1": 10, "value2": 22, "InnerInnerArray": [ "test1" , "test2"], "InnerInnerJSONArray": [{"fld1" : "val1"} , {"fld2" : "val2"}] }, "InnerJSON2":"NoneValue" } } `
輸出:
//outterJSON //innerJSON1 //InnerInnerJSONArray //Index: 0 //fld1 : val1 //Index: 1 //fld2 : val2 //value1 : 10 //value2 : 22 //InnerInnerArray //Index 0 : test1 //Index 1 : test2 //InnerJSON2 : NoneValue
這種方法有效地捕獲嵌套JSON 中的所有鍵值對,使其適合處理和Go 語言中的提取任務。
以上是如何在 Go 中高效地迭代巢狀 JSON?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

你應該關心Go語言中的"strings"包,因為它提供了處理文本數據的工具,從基本的字符串拼接到高級的正則表達式匹配。 1)"strings"包提供了高效的字符串操作,如Join函數用於拼接字符串,避免性能問題。 2)它包含高級功能,如ContainsAny函數,用於檢查字符串是否包含特定字符集。 3)Replace函數用於替換字符串中的子串,需注意替換順序和大小寫敏感性。 4)Split函數可以根據分隔符拆分字符串,常用於正則表達式處理。 5)使用時需考慮性能,如

“編碼/二進制”軟件包interingoisentialForHandlingBinaryData,oferingToolSforreDingingAndWritingBinaryDataEfficely.1)Itsupportsbothlittle-endianandBig-endianBig-endianbyteorders,CompialforOss-System-System-System-compatibility.2)

掌握Go語言中的bytes包有助於提高代碼的效率和優雅性。 1)bytes包對於解析二進制數據、處理網絡協議和內存管理至關重要。 2)使用bytes.Buffer可以逐步構建字節切片。 3)bytes包提供了搜索、替換和分割字節切片的功能。 4)bytes.Reader類型適用於從字節切片讀取數據,特別是在I/O操作中。 5)bytes包與Go的垃圾回收器協同工作,提高了大數據處理的效率。

你可以使用Go語言中的"strings"包來操縱字符串。 1)使用strings.TrimSpace去除字符串兩端的空白字符。 2)用strings.Split將字符串按指定分隔符拆分成切片。 3)通過strings.Join將字符串切片合併成一個字符串。 4)用strings.Contains檢查字符串是否包含特定子串。 5)利用strings.ReplaceAll進行全局替換。注意使用時要考慮性能和潛在的陷阱。

ThebytespackageinGoishighlyeffectiveforbyteslicemanipulation,offeringfunctionsforsearching,splitting,joining,andbuffering.1)Usebytes.Containstosearchforbytesequences.2)bytes.Splithelpsbreakdownbyteslicesusingdelimiters.3)bytes.Joinreconstructsbytesli

thealternativestogo'sbytespackageincageincludethestringspackage,bufiopackage和customstructs.1)thestringspackagecanbeusedforbytemanipulationforbytemanipulationbybyconvertingbytestostostostostostrings.2))

“字節”包裝封裝forefforeflyManipulatingByteslices,CocialforbinaryData,網絡交易和andfilei/o.itoffersfunctionslikeIndexForsearching,BufferForhandLinglaRgedLargedLargedAtaTasets,ReaderForsimulatingStreamReadReadImreAmreadReamReadinging,以及Joineffiter和Joineffiter和Joineffore

go'sstringspackageIscialforficientficientsTringManipulation,uperingToolSlikestrings.split(),strings.join(),strings.replaceall(),andStrings.contains.contains.contains.contains.contains.contains.split.split(split()strings.split()dividesStringoSubSubStrings; 2)strings.joins.joins.joinsillise.joinsinelline joinsiline joinsinelline; 3);


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

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

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。