1 回答
TA贡献1846条经验 获得超7个赞
将构造构造设置为所需的 json - 最好使用结构字段标记。非常基本的示例(有关更多信息,请阅读 https://golang.org/pkg/encoding/json/)和有关结构标签的一些信息 https://www.digitalocean.com/community/tutorials/how-to-use-struct-tags-in-go:
package main
import (
"encoding/json"
"log"
)
type KeyboardStruct struct {
Text string `json:"text"`
RequestContact bool `json:"request_contact"`
RequestLocation bool `json:"request_location"`
}
type ReplyMarkupStruct struct {
Keyboard [][]KeyboardStruct `json:"keyboard"`
ResizeKeyboard bool `json:"resize_keyboard"`
}
type ResponseStruct struct {
ChatId int `json:"chat_id"`
Text string `json:"text"`
ParseMode string `json:"parse_mode"`
ReplyMarkup ReplyMarkupStruct `json:"reply_markup"`
}
func main() {
// Example 1.
var res ResponseStruct
// Make KeyboardStructs
keyboard := [][]KeyboardStruct{
{KeyboardStruct{
Text: "",
RequestContact: false,
RequestLocation: false,
}}}
// Change in .ReplyMarkup.Keyboard
res.ReplyMarkup.Keyboard = keyboard
b, _ := json.Marshal(res)
log.Println(string(b))
// Example 2.
keyboard2 := [][]KeyboardStruct{
{KeyboardStruct{
Text: "",
RequestContact: true,
RequestLocation: false,
}}}
res2 := ResponseStruct{ChatId: 123346, Text: "test", ReplyMarkup: ReplyMarkupStruct{
keyboard2, true,
}}
b, _ = json.Marshal(res2)
log.Println(string(b))
}
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报