func (controller *OrdersController) PostAddOrder(c *gin.Context) {
var cGoodsList []models.OrderGoodsList
var tGoodsList models.OrderGoodsList
vgoodslist := c.PostFormArray("goodslist")
fmt.Println(vgoodslist)
for _, v := range vgoodslist {
json.Unmarshal([]byte(v), &tGoodsList)
cGoodsList = append(cGoodsList, tGoodsList)
}
c.JSON(200, gin.H{
"data": cGoodsList,
})
}
传递的 goodslist = [{"goodsName":"test1","format":"黑色","quantity":1,"price":99.99},{"goodsName":"test2","format":"XXL","quantity":2"price":199.99}]
最终结果:
{
"data": [
{
"goodsName": "test1",
"format": "黑色",
"quantity": 1,
"price": 99.99
},
{
"goodsName": "test1",
"format": "黑色",
"quantity": 1,
"price": 99.99
}
]
}
传递的 goodslist = [{"goodsName":"test1","format":"黑色","quantity":1,"price":99.99},{"goodsName":"test2","format":"XXL","quantity":2"price":199.99}]
2后面少了 ,
#2
更多评论
json.Unmarshal会返回错误 invalid character '"' after object key:value pair
#3