These are chat archives for gin-gonic/gin

21st
Mar 2018
Christophe Hesters
@toefel18
Mar 21 2018 06:25 UTC
rows probably is a data structure with private fields which do not get marshalled as json
I think you would need to transform/map the resultset to a struct or a slice of maps if you want to be generic
Tsingson
@tsingson
Mar 21 2018 10:25 UTC

@ostrichegret hi, please use this:

func ginSqlxJsonHandler(c *gin.Context) {

rows, err := ………….



    if err == nil {
        c.Data(http.StatusOK, gin.MIMEJSON, rows)
    }

……

}

Tsingson
@tsingson
Mar 21 2018 10:30 UTC

@ostrichegret

for this

rows, err := db.NamedQuery(SELECT * FROM lm_user WHERE user_account=:user_account, map[string]interface{}{"user_account": "ostrichegret”})

i suggest this in sql like this:

select array_to_json(array_agg(row_to_json(e.*))) as array_to_json from (

SELECT epgs.epg_id,
    epgs.channel_id,
    epgs.title,
    epgs.subtitle,
    epgs.description,
    epgs.start_time::timestamp,
    epgs.end_time::timestamp
   FROM api.epg epgs ) e;

that use array_to_json to return json ,

but, this is a suggest, i do not know anythings about db.NamedQuery in any sqx

i use postgresql 10 and github.com/JackC/pgx to bind to gin-gonic/gin for web REST at 2017

@ostrichegret the c *gin.Context then c.Data to send byte[] as output, so , make sure your rows is byte[] or somethings like this data stream
Tsingson
@tsingson
Mar 21 2018 10:42 UTC

@ostrichegret

sorry about my wrong answer,

checkout this https://github.com/jmoiron/sqlx/blob/master/named_test.go

and yet, you need rows.StructScan(&p2) and jsoniter.Marshal to JSON byte[]