在開發過程中,我們常會使用JSON格式來傳輸資料。然而,有時候我們會發現在解組JSON資料到介面後,數位資料會被截斷。這是一個很常見的問題,但卻很容易被忽略。在本文中,php小編子墨將詳細介紹這個問題的原因,以及如何避免和解決這個問題,確保數字資料的完整性和準確性。
問題內容
所以我有一個包含許多欄位的 json,我按照建議循環遍歷它 如何有效地更改 json 鍵 刪除一些我不需要的鍵。但是刪除後,現有json的原始值發生了變化,其中一些似乎是浮點數,我做了一個演示來展示它。
如何改變這種行為?是 interface{}
導致了這個問題嗎?為什麼1684366653200744506
被截斷為1684366653200744400
?
謝謝!
https://go.dev/play/p/x2auwqwb2fl
#作為參考,輸出 json 改為 1684366653200744400
2009/11/10 23:00:00 1684366653200744448.000000 2009/11/10 23:00:00 map[timestamp:1.6843666532007444e+18] 2009/11/10 23:00:00 json Marshal from maps of key string and value interface to batch json for insert to DB 2009/11/10 23:00:00 {"timestamp":1684366653200744400}
解決方法
這是因為預設情況下,encoding/json
套件將 float64
儲存在 json 數字的介面值中。請參閱 json.unmarshal:
要將 json 解組為介面值,unmarshal 將下列內容之一儲存在介面值中:
- bool,用於 json 布林值
- float64,用於 json 數字
- #...
您可以建立解碼器並呼叫 (*decoder).usenumber改變行為:
jsonBatch := `{"timestamp":1684366653200744506, "todelete":"string value or boolean value"}` dec := json.NewDecoder(strings.NewReader(jsonBatch)) dec.UseNumber() var i interface{} if err := dec.Decode(&i); err != nil {
參見https://www.php.cn/link/3a6c2c9231df58107434b942fa600b22.
以上是JSON 數字在解組到介面後被截斷的詳細內容。更多資訊請關注PHP中文網其他相關文章!

goisastrongchoiceforprojectsneedingsimplicity,績效和引發性,butitmaylackinadvancedfeatures and ecosystemmaturity.1)

Go'sinitfunctionandJava'sstaticinitializersbothservetosetupenvironmentsbeforethemainfunction,buttheydifferinexecutionandcontrol.Go'sinitissimpleandautomatic,suitableforbasicsetupsbutcanleadtocomplexityifoverused.Java'sstaticinitializersoffermorecontr

thecommonusecasesfortheinitfunctionoare:1)加載configurationfilesbeforeThemainProgramStarts,2)初始化的globalvariables和3)runningpre-checkSorvalidationsbeforEtheprofforeTheProgrecce.TheInitFunctionIsautefunctionIsautomentycalomationalmatomatimationalycalmatemationalcalledbebeforethemainfuniinfuninfuntuntion

ChannelsarecrucialingoforenablingsafeandefficityCommunicationBetnewengoroutines.theyfacilitateSynChronizationAndManageGoroutIneLifeCycle,EssentialforConcurrentProgramming.ChannelSallSallSallSallSallowSallowsAllowsEnderDendingAndReceivingValues,ActassignalsignalsforsynChronization,and actassignalsynChronization and andsupppor

在Go中,可以通過errors.Wrap和errors.Unwrap方法來包裝錯誤並添加上下文。 1)使用errors包的新功能,可以在錯誤傳播過程中添加上下文信息。 2)通過fmt.Errorf和%w包裝錯誤,幫助定位問題。 3)自定義錯誤類型可以創建更具語義化的錯誤,增強錯誤處理的表達能力。

Gooffersrobustfeaturesforsecurecoding,butdevelopersmustimplementsecuritybestpracticeseffectively.1)UseGo'scryptopackageforsecuredatahandling.2)Manageconcurrencywithsynchronizationprimitivestopreventraceconditions.3)SanitizeexternalinputstoavoidSQLinj

Go的錯誤接口定義為typeerrorinterface{Error()string},允許任何實現Error()方法的類型被視為錯誤。使用步驟如下:1.基本檢查和記錄錯誤,例如iferr!=nil{log.Printf("Anerroroccurred:%v",err)return}。 2.創建自定義錯誤類型以提供更多信息,如typeMyErrorstruct{MsgstringDetailstring}。 3.使用錯誤包裝(自Go1.13起)來添加上下文而不丟失原始錯誤信息,

對效率的Handleerrorsinconcurrentgopragrs,UsechannelstocommunicateErrors,enplionErrorWatchers,Instertimeout,UsebufferedChannels和Provideclearrormessages.1)USEchannelelStopassErtopassErrorsErtopassErrorsErrorsErrorsFromGoroutInestOthemainFunction.2)


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

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

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

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

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