当我试图从我的数据库中获取 JSON 数据时,我得到这个:
{"time":"13:42:21 11.12.14",":":"Привет"}
{"time":"13:42 :25 11.12.14",":":"Эй!"}
var time string
var text string
type chatbetweenusers struct {
Time string `json:"time"`
Text string `json:":"text"`
}
var buffer bytes.Buffer
db, err := sql.Open("sqlite3", "./database.db"); if err != nil {
log.Fatal(err)
}; defer db.Close()
rows, err := db.Query("select time, text from messages;")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
// give a names for every column from query result
err := rows.Scan(&time, &text)
if err != nil {
log.Fatal(err)
}
// add strings to struct
chatitem := &chatbetweenusers{time, text}
log.Println(chatitem)
// format it to JSON!
b, err := json.Marshal(chatitem)
// conver JSON into string to return from Martini
s := string(b[:])
buffer.WriteString(s)
}
return buffer.String()
})
当我试图从数据库中获取其他数据时,我得到了这个:
{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{ }{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
var text string
var user1 string
type chatbetweenusers struct {
Text string `json:":"text"`
User1 string `json:":"user1"`
}
var buffer bytes.Buffer
db, err := sql.Open("sqlite3", "./database.db"); if err != nil {
log.Fatal(err)
}; defer db.Close()
rows, err := db.Query("select text, user1 from messages;")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
// give a names for every column from query result
err := rows.Scan(&text, &user1)
if err != nil {
log.Fatal(err)
}
// add strings to struct
chatitem := &chatbetweenusers{text, user1}
log.Println(chatitem)
// format it to JSON!
b, err := json.Marshal(chatitem)
// conver JSON into string to return from Martini
s := string(b[:])
buffer.WriteString(s)
}
return buffer.String()
})
当我在 JSON 中合成其他数据时会发生什么?我尝试了所有可能的方法,但这不能正常工作。