使用`json.NewDecoder`读取JSON数组。

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

Read json array with json.NewDecoder

问题

你可以使用json.Unmarshal函数将JSON数组解析为切片。在你的代码中,你可以将List字段定义为[]string类型,然后使用json.Unmarshal将JSON数组解析为该字段。以下是修改后的代码示例:

type Config struct {
    Data1 struct {
        Host string `json:"host"`
        Port string `json:"port"`
    } `json:"data1"`

    Data2 struct {
        Host string `json:"host"`
        Port string `json:"port"`
    } `json:"data2"`

    List []string `json:"list"`
}

jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)

这样,List字段将会被解析为一个包含JSON数组元素的切片。在你的示例JSON中,List字段将包含四个字符串元素:"item 1"、"item 2"、"item 3"和"item 4"。

英文:

How do I get a json array (named list in the json file) to a list using json.NewDecoder? My struct looks like this:

type Config struct {
	Data1 struct {
		Host string `json:"host"`
		Port string `json:"port"`
	} `json:"data1"`

	Data2 struct {
		Host string `json:"host"`
		Port string `json:"port"`
	} `json:"data2"`

	List struct {
		Items []string
	} `json:"list"`
}

and I'm parsing like this:

jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)

my config.json looks like this

{
  "data1": {
    "host": "10.10.20.20",
    "port": "1234"
  },
  "data2": {
    "host": "10.10.30.30",
    "port": "5678"
  },
  "list": [
    "item 1",
    "item 2",
    "item 3",
    "item 4"
  ]
}

It's easy when the fields have names but I havn't figured out on how to get the information from the list...

答案1

得分: 1

我找到了解决你问题的方法。以下是代码的翻译:

package main

import (
	"encoding/json"
	"fmt"
	"strings"
)

type ConfigWithoutList struct {
	Data1 struct {
		Host string `json:"host"`
		Port string `json:"port"`
	} `json:"data1"`

	Data2 struct {
		Host string `json:"host"`
		Port string `json:"port"`
	} `json:"data2"`
}

type Config struct {
	ConfigWithoutList
	List struct {
		Items []string
	} `json:"list"`
}

func (u *Config) UnmarshalJSON(data []byte) error {
	aux := struct {
		List []string `json:"list"`
		ConfigWithoutList
	}{}
	if err := json.Unmarshal(data, &aux); err != nil {
		return err
	}
	u.List = struct {
		Items []string
	}{
		Items: aux.List,
	}
	return nil
}

func main() {
	const jsonStream = `{
  "data1": {
    "host": "10.10.20.20",
    "port": "1234"
  },
  "data2": {
    "host": "10.10.30.30",
    "port": "5678"
  },
  "list": [
    "item 1",
    "item 2",
    "item 3",
    "item 4"
  ]
}
`
	config := Config{}
	jsonParser := json.NewDecoder(strings.NewReader(jsonStream))
	jsonParser.Decode(&config)

	fmt.Println(config.List) // 输出 => {[item 1 item 2 item 3 item 4]}
}

希望对你有帮助!

英文:

I found a way to resolve your problom. here is the code:

package main

import (
	"encoding/json"
	"fmt"
	"strings"
)

type ConfigWithoutList struct {
	Data1 struct {
		Host string `json:"host"`
		Port string `json:"port"`
	} `json:"data1"`

	Data2 struct {
		Host string `json:"host"`
		Port string `json:"port"`
	} `json:"data2"`
}

type Config struct {
	ConfigWithoutList
	List struct {
		Items []string
	} `json:"list"`
}

func (u *Config) UnmarshalJSON(data []byte) error {
	aux := struct {
		List []string `json:"list"`
		ConfigWithoutList
	}{}
	if err := json.Unmarshal(data, &aux); err != nil {
		return err
	}
	u.List = struct {
		Items []string
	}{
		Items: aux.List,
	}
	return nil
}

func main() {
	const jsonStream = `{
  "data1": {
    "host": "10.10.20.20",
    "port": "1234"
  },
  "data2": {
    "host": "10.10.30.30",
    "port": "5678"
  },
  "list": [
    "item 1",
    "item 2",
    "item 3",
    "item 4"
  ]
}
`
	config := Config{}
	jsonParser := json.NewDecoder(strings.NewReader(jsonStream))
	jsonParser.Decode(&config)

	fmt.Println(config.List) // output => {[item 1 item 2 item 3 item 4]}
}

huangapple
  • 本文由 发表于 2022年9月8日 19:34:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/73648441.html
匿名

发表评论

匿名网友

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

确定