解组JSON返回空结构
这是我的JSON文件:解组JSON返回空结构,json,go,unmarshalling,Json,Go,Unmarshalling,这是我的JSON文件: { "database": { "dialect": "mysql" "host": "localhost", "user": "root", "pass": "", "name": "sws" } } 这是我的密码: package config import ( "fmt" "encoding/json" "io/ioutil" "log
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
这是我的密码:
package config
import (
"fmt"
"encoding/json"
"io/ioutil"
"log"
"os"
)
type ConfigType struct {
Database DatabaseType `json:"database"`
}
type DatabaseType struct {
Dialect string `json:"dialect"`
Host string `json:"host"`
User string `json:"user"`
Pass string `json:"pass"`
Name string `json:"name"`
}
func Config() {
file, err := os.Open("./config/config.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileBytes, _ := ioutil.ReadAll(file)
var Conf ConfigType
json.Unmarshal(fileBytes, &Conf)
fmt.Printf("File content:\n%v", string(fileBytes))
fmt.Printf("Conf: %v\n", Conf)
fmt.Printf("Content: \n %v \nType: %T", Conf.Database.Host, Conf)
}
以下是输出:
File content:
{
"database": {
"dialect": "mysql"
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{ }}
Content:
Type: config.ConfigType%
程序包被导入到
main
,只执行Config
功能。我看了很多类似的问题,似乎我的代码与答案中的代码几乎完全相同,但我无法让代码正常工作。除非你想弄清楚为什么你的应用程序不能工作,否则错误不会优雅地返回给你忽略。不要忽略错误ioutil.ReadAll()
返回错误json.Unmarshal()
返回一个错误。一定要检查那些
如果添加了错误检查,json.Unmarshal()
将返回:
panic: invalid character '"' after object key:value pair
试穿这个
您的输入JSON无效。“方言”
行中缺少逗号。添加缺少的逗号(请在上尝试):
除非你想弄清楚为什么你的应用程序不能工作,否则错误不会被优雅地返回给你去忽略。不要忽略错误
ioutil.ReadAll()
返回错误json.Unmarshal()
返回一个错误。一定要检查那些
如果添加了错误检查,json.Unmarshal()
将返回:
panic: invalid character '"' after object key:value pair
试穿这个
您的输入JSON无效。“方言”
行中缺少逗号。添加缺少的逗号(请在上尝试):
不要忽视错误。如果函数返回错误,请始终跟踪错误。它可以帮助你发现是否出了问题
> In your case json file is invalid you missed "," after "dialect": "mysql" .
> valid json file should be (config.json )
{
"database": {
"dialect": "mysql",
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
>
>
> Modified code
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)
type ConfigType struct {
Database DatabaseType `json:"database"`
}
type DatabaseType struct {
Dialect string `json:"dialect"`
Host string `json:"host"`
User string `json:"user"`
Pass string `json:"pass"`
Name string `json:"name"`
}
func main() {
file, err := os.Open("./config.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println("error reading file", err)
return
}
var Conf ConfigType
err = json.Unmarshal(fileBytes, &Conf)
if err != nil {
fmt.Println("unmarshalling error ", err)
return
}
fmt.Printf("File content:\n%v\n", string(fileBytes))
fmt.Printf("Conf: %v\n", Conf)
fmt.Printf("Content: \n %v \nType: %T", Conf.Database.Host, Conf)
}
> Output
File content:
{
"database": {
"dialect": "mysql",
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{mysql localhost root sws}}
Content:
localhost
Type: main.ConfigType
不要忽视错误。如果函数返回错误,请始终跟踪错误。它可以帮助你发现是否出了问题
> In your case json file is invalid you missed "," after "dialect": "mysql" .
> valid json file should be (config.json )
{
"database": {
"dialect": "mysql",
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
>
>
> Modified code
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)
type ConfigType struct {
Database DatabaseType `json:"database"`
}
type DatabaseType struct {
Dialect string `json:"dialect"`
Host string `json:"host"`
User string `json:"user"`
Pass string `json:"pass"`
Name string `json:"name"`
}
func main() {
file, err := os.Open("./config.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println("error reading file", err)
return
}
var Conf ConfigType
err = json.Unmarshal(fileBytes, &Conf)
if err != nil {
fmt.Println("unmarshalling error ", err)
return
}
fmt.Printf("File content:\n%v\n", string(fileBytes))
fmt.Printf("Conf: %v\n", Conf)
fmt.Printf("Content: \n %v \nType: %T", Conf.Database.Host, Conf)
}
> Output
File content:
{
"database": {
"dialect": "mysql",
"host": "localhost",
"user": "root",
"pass": "",
"name": "sws"
}
}
Conf: {{mysql localhost root sws}}
Content:
localhost
Type: main.ConfigType