」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 在GO中刪除JSON時,如何區分空白和未指定的字段?

在GO中刪除JSON時,如何區分空白和未指定的字段?

發佈於2025-03-23
瀏覽:531

How Can I Differentiate Between Void and Unspecified Fields When Unmarshaling JSON in Go?
在JSON中識別void and intscepified字段,在JSON中,JSON中的JSON

[ {“ name”:“ a”,“ description”:“ monotremata”}, {“ name”:“ b”}, {“名稱”:“ C”,“描述”:“”} ] 如果我們定義了這樣的GO結構:

and unmarshal the JSON into a slice of Category instances, we get the following output:

[{Name:A Description:Monotremata} {Name:B Description:} {Name:C Description:}]

Notice that the Description field of B is an empty string, while the Description field of C is completely omitted from the JSON.在這兩種情況下,描述字段都設置為GO表示中的一個空字符串。
[
  {"Name": "A", "Description": "Monotremata"},
  {"Name": "B"},
  {"Name": "C", "Description": ""}
]
很大,一種方法是一種方法,一種方法是將指針用於可選字段。通過將描述類型更改為指針,我們可以區分空字符串值和零值(指示未指定的字段):

類型類別struct { 名稱字符串 描述 *字符串 } [&& && && && && && &&華描述C的字段是指向空字符串的指針(由十六進制內存地址指示)。這使我們能夠識別未指定的字段並在我們的程序中相應處理。

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3