嵌套映射 return json 数组[]
Nested map return json array[]
如何创建嵌套的 JSON 数组?还有其他简单的方法吗?
我试过这个:
var m1 = make(map[string]interface{})
m1 = append(tickets, ptotal) //error is here
i.Data["json"] = m1
i.ServeJSON()
但它显示此错误:
cannot use append(tickets, ptotal) (type []interface {}) as type map[string]interface {} in assignment
我可以这样做,但根本没有嵌套:
// some := append(tickets, map[string]int64{"totalpage": ptotal})
我想要这个 JSON:
[
{tickets
name:value,
},
],
[
{
totalpage: 10
}
]
var tickets []interface{}
var ptotal int64
tickets, ptotal, err := models.GetAllTicket(query, fields, sortby, order, offset, limit)
if err != nil {
// bulabula
}
i.Data["json"] = append(tickets, map[string]int64{"totalpage": ptotal})
试试这个:
var data = map[string]map[string]string{
"ticket": map[string]string{},
"totalpage": map[string]string{},
"c": map[string]string{},
}
data["ticket"]["w"] = "vrniairvnai"
data["totalpage"]["w"] = "ten"
fmt.Println(data)
var data2 = make(map[string]interface{})
data2["data"] = data
data2["int"] = 10
fmt.Println(data2)
如何创建嵌套的 JSON 数组?还有其他简单的方法吗?
我试过这个:
var m1 = make(map[string]interface{})
m1 = append(tickets, ptotal) //error is here
i.Data["json"] = m1
i.ServeJSON()
但它显示此错误:
cannot use append(tickets, ptotal) (type []interface {}) as type map[string]interface {} in assignment
我可以这样做,但根本没有嵌套:
// some := append(tickets, map[string]int64{"totalpage": ptotal})
我想要这个 JSON:
[
{tickets
name:value,
},
],
[
{
totalpage: 10
}
]
var tickets []interface{}
var ptotal int64
tickets, ptotal, err := models.GetAllTicket(query, fields, sortby, order, offset, limit)
if err != nil {
// bulabula
}
i.Data["json"] = append(tickets, map[string]int64{"totalpage": ptotal})
试试这个:
var data = map[string]map[string]string{
"ticket": map[string]string{},
"totalpage": map[string]string{},
"c": map[string]string{},
}
data["ticket"]["w"] = "vrniairvnai"
data["totalpage"]["w"] = "ten"
fmt.Println(data)
var data2 = make(map[string]interface{})
data2["data"] = data
data2["int"] = 10
fmt.Println(data2)