Golang 中,您可以使用标准库中的 encoding/json 包来解析 JSON 格式的配置文件。 首先,您需要确定配置文件的结构。假设您的配置文件包含以下信息: ``` { "server_list": [ { "id": 1, "ip": "192.168.0.1", "username": "user1" }, { "id": 2, "ip": "192.168.0.2", "username": "user2" } ] } ``` 您可以定义一个结构体来存储这些信息: ``` type Config struct { ServerList []struct { ID int `json:"id"` IP string `json:"ip"` Username string `json:"username"` } `json:"server_list"` } ``` 然后,您可以使用 json.Unmarshal 函数来从文件读取配置信息并解析到 Config 结构体中: ``` func main() { // Open the config file. file, err := os.Open("config.json") if err != nil { fmt.Println(err) return } defer file.Close() // Read the config file into a byte slice. b, err := ioutil.ReadAll(file) if err != nil { fmt.Println(err) return } // Unmarshal the JSON data into a Config struct. var config Config err = json.Unmarshal(b, &config) if err != nil { fmt.Println(err) return } // Access the config values. fmt.Println(config.ServerList[0].IP) fmt.Println(config.ServerList[1].Username) } ``` 在这个例子中,我们打开了一个名为 "config.json" 的文件读取了其中的内容并将其存储到一个字节片中,然后使用 json.Unmarshal 函数将 JSON 数据解析到 Config 结构体中。最后,