我正在尝试设置一个从数据库查询数据并将其作为 JSON 发送的 Go MySQL 服务器。我的数据库包含一些新 JSON 类型的列。
地图结构:
type Map struct{
Id int `json:"id"`
Data string `json:"data"` //This column is stored in the database as a JSON. Which type to use here?
Created time.Time `json:"created"`
UserId int `json:userid`
}
从数据库中获取数据的函数
func GetMap(id int) Map{
var mapId int
var data string //which type should this be
var userId int
var created time.Time
err := _getMap.QueryRow(id).Scan(&mapId, &data, &userId, &created) //getMap is a prepared query
mapObj := Map{Id:mapId, Data:data, UserId:userId, Created:created}
return mapObj
}
当我像这样发送这些数据时
resp.Header().Set("Content-Type", "application/json; charset=UTF-8")
resp.WriteHeader(http.StatusOK)
gmap := GetMap(id)
err := json.NewEncoder(resp).Encode(gmap);
客户端收到的 JSON 中的数据被格式化为字符串:
{\"field\":\"nowork\"...}
我认为问题是由结构定义中的错误数据类型引起的,这意味着数据将以错误的格式解析。
我试图将数据类型更改为 []uint8、interface{} 和其他一些我认为可能相关的数据类型,但无济于事。
HUH函数
相关分类