英文:
How to convert JSON body to Golang types?
问题
我已经使用这个工具 - https://mholt.github.io/json-to-go/ - 将 JSON 数据转换为 Golang 结构。
输入的 JSON
{
"name": "my_dns",
"servername": "example.com",
"allow": [
{
"ip": "10.132.146.79",
"port": "5555"
}
],
"restrict": [
{
"ip": "10.132.146.134",
"port": "2323"
}
],
"deny": [
{
"ip": "10.132.146.201",
"port": "10001"
}
]
}
对应的 Golang 类型
type AutoGenerated struct {
Name string `json:"name,omitempty"`
Servername string `json:"servername,omitempty"`
Allow []Allow `json:"allow,omitempty"`
Restrict []Restrict `json:"restrict,omitempty"`
Deny []Deny `json:"deny,omitempty"`
}
type Allow struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
type Restrict struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
type Deny struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
问题
是否有一种方式可以定义:
type PortIP struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
并在 AutoGenerated 中使用上述类型:
type AutoGenerated struct {
Name string `json:"name,omitempty"`
Servername string `json:"servername,omitempty"`
Allow []PortIP `json:"allow,omitempty"`
Restrict []PortIP `json:"restrict,omitempty"`
Deny []PortIP `json:"deny,omitempty"`
}
如何在 Golang 中有效地解析我的 JSON 请求体(输入严格按照上述格式)?
英文:
I have used this tool - https://mholt.github.io/json-to-go/ - to convert JSON body to Golang structure.
Input JSON
{
"name": "my_dns",
"servername": "example.com",
"allow": [
{
"ip": "10.132.146.79",
"port": "5555"
}
],
"restrict": [
{
"ip": "10.132.146.134",
"port": "2323"
}
],
"deny": [
{
"ip": "10.132.146.201",
"port": "10001"
}
]
}
Corresponding Golang types
type AutoGenerated struct {
Name string `json:"name,omitempty"`
Servername string `json:"servername,omitempty"`
Allow []Allow `json:"allow,omitempty"`
Restrict []Restrict `json:"restrict,omitempty"`
Deny []Deny `json:"deny,omitempty"`
}
type Allow struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
type Restrict struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
type Deny struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
Question
Is there not a way where I can define :
type PortIP struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
And use the above type in AutoGenerated:
type AutoGenerated struct {
Name string `json:"name,omitempty"`
Servername string `json:"servername,omitempty"`
Allow []PortIP `json:"allow,omitempty"`
Restrict []PortIP `json:"restrict,omitempty"`
Deny []PortIP `json:"deny,omitempty"`
}
How can I parse my JSON body request (the input is strictly supposed to be like that) effectively in Golang?
答案1
得分: 2
要解析 JSON 输入,您可以使用 https://pkg.go.dev/encoding/json
您提供的类型似乎是正确的,这是一个完整(简约)的示例:
package main
import (
"encoding/json"
"fmt"
"log"
)
type PortIP struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
type IPRule struct {
Name string `json:"name,omitempty"`
Servername string `json:"servername,omitempty"`
Allow []PortIP `json:"allow,omitempty"`
Restrict []PortIP `json:"restrict,omitempty"`
Deny []PortIP `json:"deny,omitempty"`
}
func main() {
data := []byte(`
{
"name": "my_dns",
"servername": "example.com",
"allow": [
{
"ip": "10.132.146.79",
"port": "5555"
}
],
"restrict": [
{
"ip": "10.132.146.134",
"port": "2323"
}
],
"deny": [
{
"ip": "10.132.146.201",
"port": "10001"
}
]
}`)
var parsed IPRule
if err := json.Unmarshal(data, &parsed); err != nil {
log.Fatal("Failed to parse JSON", err)
}
fmt.Println(parsed)
}
希望对您有所帮助。
英文:
To parse a json input, you can use https://pkg.go.dev/encoding/json
The types you provide seem correct, a complete (minimalistic) example :
package main
import (
"encoding/json"
"fmt"
"log"
)
type PortIP struct {
IP string `json:"ip,omitempty"`
Port string `json:"port,omitempty"`
}
type IPRule struct {
Name string `json:"name,omitempty"`
Servername string `json:"servername,omitempty"`
Allow []PortIP `json:"allow,omitempty"`
Restrict []PortIP `json:"restrict,omitempty"`
Deny []PortIP `json:"deny,omitempty"`
}
func main() {
data := []byte(`
{
"name": "my_dns",
"servername": "example.com",
"allow": [
{
"ip": "10.132.146.79",
"port": "5555"
}
],
"restrict": [
{
"ip": "10.132.146.134",
"port": "2323"
}
],
"deny": [
{
"ip": "10.132.146.201",
"port": "10001"
}
]
}`)
var parsed IPRule
if err := json.Unmarshal(data, &parsed); err != nil {
log.Fatal("Failed to parse JSON", err)
}
fmt.Println(parsed)
}
Hope it helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论