Rumah > Artikel > pembangunan bahagian belakang > Mengapa Program My Go Menghuraikan JSON Secara Tidak Betul?
Parsing JSON into a Struct in Go
You want to parse a JSON file into a Go struct, but the program outputs incorrect values.
Issue
The struct elements are not exported, starting with lowercase letters. The JSON encoder/decoder ignores non-exported elements.
Solution
Export the struct elements by making the first letter uppercase:
type Settings struct { ServerMode bool `json:"serverMode"` SourceDir string `json:"sourceDir"` TargetDir string `json:"targetDir"` }
json:"..." tags instruct the decoder to map JSON keys to struct elements.
Updated Code
var settings Settings // ... (rest of the code)
Additional Notes
Atas ialah kandungan terperinci Mengapa Program My Go Menghuraikan JSON Secara Tidak Betul?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!