Go的json.Unmarshal函数返回false结构体。

huangapple go评论79阅读模式
英文:

Go json.Unmarshal returns false struct

问题

Golang新手在这里。

我正在尝试从一个与Go代码在同一目录下的.json文件中解析数据到一个包含其他结构体的结构体中,但我最接近成功的是一个包含布尔值false的结构体,这对我来说似乎是有问题的。

以下是我目前在Go代码中的内容:

package main

import (
	"encoding/json"
	"fmt"

	"io/ioutil"
)

type App struct {
	Name string `json:"app:name"`
}

type Database struct {
	Type     string `json:"database:type"`
	Name     string `json:"database:name"`
	User     string `json:"database:user"`
	Password string `json:"database:password"`
}

type Environment struct {
	Mode  string `json:"environment:mode"`
	Debug bool   `json:"environment:debug"`
}

type Config struct {
	Environment Environment
	App         App
	Database    Database
}

func main() {
	config, err := ioutil.ReadFile("config.json")
	if err != nil {
		fmt.Errorf("Error reading config file: %s", err)
	}

	var appSettings Config
	json.Unmarshal(config, &appSettings)

	fmt.Print(appSettings)
}

这是我的.json文件的内容:

{
  "App": {
    "Name": "My_Project"
  },
  "Database": {
    "Type": "postgres",
    "Name": "my_project_db_name",
    "User": "my_project_db_user",
    "Password": "secret"
  },
  "Environment": {
    "Mode": "development",
    "Debug": true
  }
}

编辑:
这是main()函数末尾打印的结果:

{{ false} {} { }}

我已经验证了json内容,一切都正常。结构体名称和属性已经被导出。你能看出我做错了什么吗?

英文:

Golang newbie here

I'm trying to parse from a .json file (in the same directory as the Go code) into a struct holding other structs and the closest I can get to success is a struct containing boolean false, which sounds broken to me.

Here's what I have in my Go code so far

package main

import (
	"encoding/json"
	"fmt"

	"io/ioutil"
)

type App struct {
	Name string `json:"app:name"`
}

type Database struct {
	Type     string `json:"database:type"`
	Name     string `json:"database:name"`
	User     string `json:"database:user"`
	Password string `json:"database:password"`
}

type Environment struct {
	Mode  string `json:"environment:mode"`
	Debug bool   `json:"environment:debug"`
}

type Config struct {
	Environment Environment
	App         App
	Database    Database
}

func main() {
	config, err := ioutil.ReadFile("config.json")
	if err != nil {
		fmt.Errorf("Error reading config file: %s", err)
	}

	var appSettings Config
	json.Unmarshal(config, &appSettings)

	fmt.Print(appSettings)
}

and here's the contents of my .json file

{
  "App": {
    "Name": "My_Project"
  },
  "Database": {
    "Type": "postgres",
    "Name": "my_project_db_name",
    "User": "my_project_db_user",
    "Password": "secret"
  },
  "Environment": {
    "Mode": "development",
    "Debug": true
  }
}

EDIT:
Here's the result of the print at the end of main()

{{ false} {} { }}

I have already validated the json content which is fine. The struct names and properties are being exported. Can you see what I'm doing wrong?

答案1

得分: 6

你可以尝试像这样进行更改:

type App struct {
    Name string `json:"name"`
}

type Database struct {
    Type     string `json:"type"`
    Name     string `json:"name"`
    User     string `json:"user"`
    Password string `json:"password"`
}

type Environment struct {
    Mode  string `json:"mode"`
    Debug bool   `json:"debug"`
}

这是输出结果:

Environment{Mode:"development", Debug:true} Database{Type:"", Name:"My_Project", User:"postgres", Password:"secret"}

以下是一些方便参考的文档:

  • Field 字段被此包忽略。
  • Field 字段在 JSON 中以键 "myName" 出现。
  • Field 字段在 JSON 中以键 "myName" 出现,如果其值为空,则从对象中省略该字段,如上所定义。
  • Field 字段在 JSON 中以键 "Field"(默认)出现,但如果为空,则跳过该字段。
    请注意前导逗号。
  • Field 字段在 JSON 中以键 "Field"(默认)出现,但如果为空,则跳过该字段。
    请注意前导逗号。
英文:

Can you try by changing like this:

type App struct {
    Name string `json:"name"`
}

type Database struct {
    Type     string `json:"type"`
    Name     string `json:"name"`
    User     string `json:"user"`
    Password string `json:"password"`
}

type Environment struct {
    Mode  string `json:"mode"`
    Debug bool   `json:"debug"`
}

Here is the output:

{{development true} {My_Project} {postgres my_project_db_name my_project_db_user secret}}

Here is a little docs for handy reference:

// Field is ignored by this package.
Field int `json:"-"`

// Field appears in JSON as key "myName".
Field int `json:"myName"`

// Field appears in JSON as key "myName" and
// the field is omitted from the object if its value is empty,
// as defined above.
Field int `json:"myName,omitempty"`

// Field appears in JSON as key "Field" (the default), but
// the field is skipped if empty.
// Note the leading comma.
Field int `json:",omitempty"`

huangapple
  • 本文由 发表于 2017年1月9日 02:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/41536383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定