加载 JSON 数组配置

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

Load json array configuration

问题

我想要将 JSON 配置文件加载到 Go 语言应用程序中。
配置数据是一个数组,因为它需要动态设置。

[
    { "key": "A", "data": [1, 2, 3]},
    { "key": "B", "data": [1, 2]},
    { "key": "C", "data": [1, 3]}
]

我尝试像这样加载:

package main

import (
	"flag"
	"fmt"
	"os"
	"encoding/json"
)

type ColInfo struct {
	key  string `json:"key"`
	col  []int  `json:"data"`
}

type Config struct {
	colInfos []ColInfo
}

func main() {
	flag.Parse()
	file, _ := os.Open("col.conf")
	decoder := json.NewDecoder(file)
	configuration := Config{}
	if err := decoder.Decode(&configuration); err != nil {
		fmt.Println(err)
	}
	fmt.Println(configuration.colInfos[0].key)
}

这是我得到的错误:

./test2.go:23: multiple-value json.Marshal() in single-value context

我在哪里出错了?

英文:

I would like to load json configuration file to go lang app.
Configuration data is array since it needs to be dynamically set.

> [
> { "key": "A", "data": [1, 2, 3]},
> { "key": "B", "data": [1, 2]},
> { "key": "C", "data": [1, 3]} ]

And tried to load like this.

package main

import (
	"flag"
	"fmt"
	"os"
	"encoding/json"
)

type ColInfo struct {
	key		string	`json:"key"`
    col 	[]int	`json:"data"`
}
type Config struct {
    colInfos    []ColInfo
}

func main() {
	flag.Parse()
	file, _ := os.Open("col.conf")
	decoder := json.Marshal(file)
	configuration := Config{}
	if err := decoder.Decode(&configuration); err != nil {
		fmt.Println(err)
	}
	println( configuration.colInfos[0].key)
}

Here is error I've got

> ./test2.go:23: multiple-value json.Marshal() in single-value context

What am i wrong with this?

答案1

得分: 1

你应该使用json.Unmarshal()来填充你的配置结构体。

file, err := ioutil.ReadFile("./col.conf")
if err != nil {
    fmt.Printf("文件错误:%v\n", err)
    os.Exit(1)
}
cfg := Config{}
json.Unmarshal(file, &cfg)
英文:

You should use json.Unmarshal() to populate your config struct.

file, err := ioutil.ReadFile("./col.conf")
if err != nil {
    fmt.Printf("File error: %v\n", err)
    os.Exit(1)
}
cfg := Config{}
json.Unmarshal(file, &cfg)

答案2

得分: 1

你需要更改你的"ColInfo"结构体的键,以便"json"包能够读取它们。我附上了一个可工作的代码片段:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

type ColInfo struct {
    Key string `json:"key"`
    Col []int  `json:"data"`
}

type Config struct {
    ColInfos []ColInfo
}

func main() {
    file, err := ioutil.ReadFile("configurtaion.txt")
    if err != nil {
        fmt.Printf("File error: %v\n", err)
        os.Exit(1)
    }

    cfg := Config{}
    json.Unmarshal(file, &cfg.ColInfos)
    fmt.Println(cfg.ColInfos[0])
}

请注意,我已经将结构体中的colInfos改为了ColInfos,以符合Go语言的命名约定。

英文:

You need to change your "ColInfo" struct keys so that "json" package can read them. I'm attaching a working code snippet

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

type ColInfo struct {
    Key string `json:"key"`
    Col []int  `json:"data"`
}
type Config struct {
    colInfos []ColInfo
}

func main() {
    file, err := ioutil.ReadFile("configurtaion.txt")
    if err != nil {
        fmt.Printf("File error: %v\n", err)
        os.Exit(1)
    }
    cfg := Config{}
    json.Unmarshal(file, &cfg.colInfos)
    fmt.Println(cfg.colInfos[0])
}

huangapple
  • 本文由 发表于 2017年7月18日 10:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/45156553.html
匿名

发表评论

匿名网友

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

确定