<分区>
我在解码从 .json
文件中读取的 json 数据时遇到问题
type redisConfig struct {
host string
password string
}
func loadRedisConfig() (redisConfig, error){
b, _ := ioutil.ReadFile("config.json")
var config redisConfig
fmt.Println(b)
fmt.Println(string(b))
e := json.Unmarshal(b, &config)
fmt.Println(e)
fmt.Println(config)
return config, nil;
}
文件 config.json
包含以下内容:
{
"host": "example.com",
"password": "password"
}
我已经使用 http://jsonlint.com/ 验证它是有效的 JSON .在 SO 上阅读其他类似问题,我看到问题是无效的 json,我认为这里不是这种情况。
这是运行代码片段的输出:
[123 13 10 32 32 34 104 111 115 116 34 58 32 34 101 120 97 109 112 108 101 46 99 111 109 34 44 13 10 32 32 34 112 97 115 115 119 111 114 100 34 58 32 34 112 97 115 115 119 111 114 100 34 13 10 125]
{
"host": "example.com",
"password": "password"
}
<nil>
{ }
config
变量包含一个空结构,应该用文件和解码器提供的编码 json 填充它