如何使用go struct制作复杂的json数据? [英] How to make complex json data with go struct?
本文介绍了如何使用go struct制作复杂的json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想构建一个json
I want to build a json like
{
"data": {
"posts": [
{"title": "first", "body": "first body"},
{"title": "second", "body": "second body"}
],
"categories": [
{"name": "red"},
{"name": "white"}
]
}
}
它包括数据的两个部分:帖子和类别.它们是不同的结构.但是想一次将它们一起发送.
It includes two parts of data: post and category. They are different structure. But want to send them together one time.
使用此代码
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
type Article struct {
Data struct {
Post []struct {
Title string `json:"title"`
Body string `json:"body"`
} `json:"posts"`
Category []struct {
Name string `json:"name"`
} `json:"categories"`
} `json:"data"`
}
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
article := &Article{}
data := article.Data
post := data.Post
post[0].Title = "first"
post[0].Body = "first body"
post[1].Title = "second"
post[1].Body = "second body"
category := data.Category
category[0].Name = "red"
category[1].Name = "white"
return c.JSON(http.StatusOK, article)
})
e.Logger.Fatal(e.Start(":8008"))
}
我尝试制作根结构 Article
,然后定义其子级,然后将数据一一设置为树.
I tried to make a root struct Article
, then define its children, then set data to tree one by one.
运行服务器并调用/端点时,出现错误
When run the server and call / endpoint, got error
echo: http: panic serving [::1]:49501: runtime error: index out of range [0] with length 0
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc00012afa0)
/Users/user/.goenv/versions/1.15.7/src/net/http/server.go:1801 +0x147
panic(0x1314380, 0xc00001a200)
/Users/user/.goenv/versions/1.15.7/src/runtime/panic.go:975 +0x47a
main.main.func1(0x139b160, 0xc000208000, 0x0, 0x0)
/Users/user/test/testecho/server.go:28 +0x13b
github.com/labstack/echo/v4.(*Echo).add.func1(0x139b160, 0xc000208000, 0x0, 0x0)
/Users/user/go/1.15.7/pkg/mod/github.com/labstack/echo/v4@v4.2.0/echo.go:536 +0x62
github.com/labstack/echo/v4.(*Echo).ServeHTTP(0xc000180000, 0x1397680, 0xc000200000, 0xc000072000)
/Users/user/go/1.15.7/pkg/mod/github.com/labstack/echo/v4@v4.2.0/echo.go:646 +0x187
然后尝试了
e.GET("/", func(c echo.Context) error {
article := &Article{
Data: struct{
"Post": {
{
"Title": "first",
"Body": "first body",
},
{
"Title": "second",
"Body": "second body",
},
},
"Category": {
{
"Name": "red",
},
{
"Name": "white",
},
},
}
a, err := json.Marshal(article)
if err != nil {
fmt.Println(err)
}
return c.JSON(http.StatusOK, a)
})
但是 Data:struct {
行得到了期望的表达式
.
推荐答案
您可以通过为输出中的每个对象声明一个命名类型来简化所需的代码:
You can simplify the code required by declaring a named type for each object in the output:
type Post struct {
Title string `json:"title"`
Body string `json:"body"`
}
type Category struct {
Name string `json:"name"`
}
type Data struct {
Posts []Post `json:"posts"`
Categories []Category `json:"categories"`
}
type Article struct {
Data Data
}
通过此更改,可以很容易地编写复合文字:
With this change, it's easy to write the composite literal:
article := &Article{
Data: Data{Posts: []Post{
{
Title: "first",
Body: "first body",
},
{
Title: "second",
Body: "second body",
},
},
Categories: []Category{
{
Name: "red",
},
{
Name: "white",
},
},
},
}
这篇关于如何使用go struct制作复杂的json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文