习惯了PHP的查询方式,百度搜索到了如下代码(DoQuery 函数),
查询之后,直接返回 所有结果 map
控制台输出的时候:
[map[ue_account:[49 51 56 56 56 56 56 56 56 56 56] ue_id:[49] ue_yqm:[97 110 115 56 56 56 56 54 55 56 57] zhxm:[230 157 142 229 176 143 233 190 153]] map[ue_account:[49 51 54 54 54 54 54 54 54 54 54] ue_id:[51] ue_yqm:[97 110 115 53 56 50 55 52 57 57 48 50 51] zhxm:[230 157 142 228 186 140 233 190 153]] map[ue_account:[49 51 51 51 51 51 51 51 51 51 51] ue_id:[52] ue_yqm:[97 110 115 53 56 50 55 57 56 51 53 52 55] zhxm:[230 157 142 228 184 137 233 190 153]] map[ue_account:[49 51 53 53 53 53 53 53 53 53 53] ue_id:[53] ue_yqm:[97 110 115 53 56 50 55 54 56 51 48 52 52] zhxm:[230 157 142 233 190 153 228 186 148]] map[ue_account:[49 51 55 55 55 55 55 55 55 55 55] ue_id:[54] ue_yqm:[97 110 115 53 56 50 55 54 57 52 52 55 48] zhxm:[230 157 142 233 190 153 229 133 173]] map[ue_account:[49 51 49 49 49 49 49 49 49 49 49] ue_id:[56] ue_yqm:[97 110 115 53 56 50 55 51 51 55 55 55 49] zhxm:[230 157 142 233 190 153 229 133 171]] map[ue_account:[49 51 50 50 50 50 50 50 50 50 50] ue_id:[57] ue_yqm:[97 110 115 53 56 50 55 49 52 57 50 52 54] zhxm:[230 157 142 233 190 153 228 185 157]] map[ue_account:[49 56 57 55 53 55 49 53 56 50 55] ue_id:[49 48] ue_yqm:[97 110 115 53 56 50 55 57 55 51 49 54 53] zhxm:[230 157 142 228 184 128]]]
其中 ue_account 都是 数字,手机号
ue_yqm 都是字母 ue_id 是数字 ,zhxm 是中文。。现在这显示的都是 unicode编码。
输出json格式 这其中的值,全部都被base64编码了!
[{"ue_account":"MTM4ODg4ODg4ODg=","ue_id":"MQ==","ue_yqm":"YW5zODg4ODY3ODk=","zhxm":"5p2O5bCP6b6Z"},{"ue_account":"MTM2NjY2NjY2NjY=","ue_id":"Mw==","ue_yqm":"YW5zNTgyNzQ5OTAyMw==","zhxm":"5p2O5LqM6b6Z"},{"ue_account":"MTMzMzMzMzMzMzM=","ue_id":"NA==","ue_yqm":"YW5zNTgyNzk4MzU0Nw==","zhxm":"5p2O5LiJ6b6Z"},{"ue_account":"MTM1NTU1NTU1NTU=","ue_id":"NQ==","ue_yqm":"YW5zNTgyNzY4MzA0NA==","zhxm":"5p2O6b6Z5LqU"},{"ue_account":"MTM3Nzc3Nzc3Nzc=","ue_id":"Ng==","ue_yqm":"YW5zNTgyNzY5NDQ3MA==","zhxm":"5p2O6b6Z5YWt"},{"ue_account":"MTMxMTExMTExMTE=","ue_id":"OA==","ue_yqm":"YW5zNTgyNzMzNzc3MQ==","zhxm":"5p2O6b6Z5YWr"},{"ue_account":"MTMyMjIyMjIyMjI=","ue_id":"OQ==","ue_yqm":"YW5zNTgyNzE0OTI0Ng==","zhxm":"5p2O6b6Z5Lmd"},{"ue_account":"MTg5NzU3MTU4Mjc=","ue_id":"MTA=","ue_yqm":"YW5zNTgyNzk3MzE2NQ==","zhxm":"5p2O5LiA"}]
就是这样。
请教怎么输出正常的中文 和 字母 数字
rows, _ := DoQuery(db, "SELECT ue_id,ue_account,ue_yqm,zhxm FROM ak_user")
b, err := json.Marshal(rows)
if err != nil {
fmt.Println("json err:", err)
}
fmt.Fprintf(w, string(b))
func DoQuery(db *sql.DB, sqlInfo string, args ...interface{}) ([]map[string]interface{}, error) {
rows, err := db.Query(sqlInfo, args...)
if err != nil {
return nil, err
}
columns, _ := rows.Columns()
columnLength := len(columns)
cache := make([]interface{}, columnLength) //临时存储每行数据
for index, _ := range cache { //为每一列初始化一个指针
var a interface{}
cache[index] = &a
}
var list []map[string]interface{} //返回的切片
for rows.Next() {
_ = rows.Scan(cache...)
item := make(map[string]interface{})
for i, data := range cache {
fmt.Println(columns[i])
item[columns[i]] = *data.(*interface{}) //取实际类型
}
list = append(list, item)
}
_ = rows.Close()
return list, nil
}
有疑问加站长微信联系(非本文作者)