Answer the question
In order to leave comments, you need to log in
Why is an empty list returned in json?
Good evening.
Tell me, pliz, good people. The following code in the browser returns: {"Articles":[{},{},{}]}
What's wrong...
type Article struct {
id string `json: "id"`
title string `json: "title"`
pretext string `json: "pretext"`
text string `json: "text"`
}
type Articles struct {
Articles []Article `json: "article"`
}
var conn *sql.DB
func GetArticles() Articles {
result := Articles{}
conn := db.CreateConn()
defer conn.Close()
sqlStatement := "SELECT id, title, pretext, text FROM content order by id LIMIT 0, 3"
rows, err := conn.Query(sqlStatement)
defer rows.Close()
for rows.Next() {
article := Article{}
err := rows.Scan(&article.id, &article.title, &article.pretext, &article.text)
fmt.Println(article.id) // id выводится
result.Articles = append(result.Articles, article)
}
return result
}
func GetArticles(c echo.Context) error {
result := models.GetArticles()
fmt.Println(result); // выводит следующее (не похоже на json): {[{12 Название Статья}, {13 Название Статья}]}
return c.JSON(http.StatusOK, result)
}
Answer the question
In order to leave comments, you need to log in
You have fields in the struct with a lowercase letter, which means they are private and will not be exported to json.
Try like this:
type Article struct {
Id string `json: "id"`
Title string `json: "title"`
Pretext string `json: "pretext"`
Text string `json: "text"`
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question