I'm trying to create the following json below, but I'm not getting it:
json
{
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "foo1",
"displayText": "foo2"
}
},
{
"basicCard": {
"formattedText": "foo3",
"imageDisplayOptions": "CROPPED"
}
}
]
}
}
The problem is that I have a []struct
calledItems
(slice of structs) and it has 2 different structs SimpleResponse
andBasicCard
and I am not able to mount this json.
The errors appear:
can not use literal SimpleResponse
can not use BasicCard literal
main.go
https://play.golang.org/p/Gbl0UNWhqko
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
type SimpleResponse struct {
TextToSpeech string `json:"textToSpeech"`
DisplayText string `json:"displayText"`
}
type BasicCard struct {
FormattedText string `json:"formattedText"`
ImageDisplayOptions string `json:"imageDisplayOptions"`
}
type Items []struct {
SimpleResponse SimpleResponse `json:"simpleResponse"`
BasicCard BasicCard `json:"basicCard"`
}
type RichResponse struct{
Items Items `json:"items"`
}
group := RichResponse{
Items: Items{
SimpleResponse{"foo1", "foo2"},
BasicCard{
FormattedText: "foo3",
ImageDisplayOptions: "CROPPED",
},
},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
}
Could you help me mount this json?