英文:
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]}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论