从mongodb检索到数组对象。数据如下所示: -
[{
1
fruits
Apple
Apple is my favorite fruit.
}
{
2
colors
Red
Red color is always charming.
}
{
3
flowers
Lotus
It is one of the most beautiful flowers in this world.
}]
这是检索上述数据的代码 结构是:
type Item struct {
Id int `json:"id"`
Category string `json:"category"`
Name string `json:"name"`
Description string `json:"description"`
}
type Items []Item
func GetData(Query interface{}) (result Items, err error) {
mongoSession := ConnectDb()
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
getCollection := mongoSession.DB("custom").C("custom")
err = getCollection.Find(Query).All(&result)
if err != nil {
return result, err
}
return result, nil
}
/*
* Retrieve the data used by main function
*/
func retrieve(c *gin.Context) {
//mongoSession := ConnectDb()
conditions := bson.M{}
data, err :=GetData(conditions)
if err != nil {
fmt.Println("There is somthing wrong")
}
arrange(data)
return
}
func arrange(data Items) {
pagesJson, err := json.Marshal(data)
if err != nil {
log.Fatal("Cannot encode to JSON ", err)
}
fmt.Println(string(pagesJson))
}
运行json.Marshal
,输出如下所示:
[{
"id":1,
"category":"fruits",
"name":"Apple",
"description":"Apple is my favorite fruit."
},
{
"id":2,
"category":"colors",
"name":"Red",
"description":"Red color is always charming."
},
{
"id":3,
"category":"flowers",
"name":"Lotus",
"description":"It is one of the most beautiful flowers in this world."
}]
预期输出
{
"id":1,
"category":"fruits",
"name":"Apple",
"description":"Apple is my favorite fruit."
}
{
"id":2,
"category":"colors",
"name":"Red",
"description":"Red color is always charming."
}
{
"id":3,
"category":"flowers",
"name":"Lotus",
"description":"It is one of the most beautiful flowers in this world."
}
问题是数据在我使用的数组对象中我需要{}
之间的字符串结构数据,如上所示。我之前发过这个问题,但没有得到任何成功的答案。我从很多时候开始尝试帮助我,谢谢你。
答案 0 :(得分:1)
根据OP讨论的内容,对结构Items
进行编组将得到:
[{
"id":1,
"category":"fruits",
"name":"Apple",
"description":"Apple is my favorite fruit."
},
{
"id":2,
"category":"colors",
"name":"Red",
"description":"Red color is always charming."
},
{
"id":3,
"category":"flowers",
"name":"Lotus",
"description":"It is one of the most beautiful flowers in this world."
}]
由此,OP希望得到一个如下所示的字符串:
{
"id":1,
"category":"fruits",
"name":"Apple",
"description":"Apple is my favorite fruit."
},
{
"id":2,
"category":"colors",
"name":"Red",
"description":"Red color is always charming."
},
{
"id":3,
"category":"flowers",
"name":"Lotus",
"description":"It is one of the most beautiful flowers in this world."
}
我考虑过两种方法:
循环浏览项目并将每个封送的项目附加到字符串中(显然效率不高)
func getMyString(items Items) (string, error) {
var buffer bytes.Buffer
var err error
var b []byte
for _, item := range items {
b, err = json.Marshal(item)
if err != nil {
return "", err
}
buffer.WriteString(string(b) + ",")
}
s := strings.TrimSpace(buffer.String())
// trim last comma
s = s[:len(s)-1]
return s, nil
}
或者只是修剪前后方括号:
func getMyString2(items Items) (string, error) {
b, err := json.Marshal(items)
if err != nil {
return "", err
}
s := string(b)
s = strings.TrimSpace(s)
// trim leading and trailing spaces
s = s[1 : len(s)-1]
return s, nil
}
代码链接:https://play.golang.org/p/F1SUYJZEn_n
编辑:由于OP希望它与众不同,我修改了第1号方法以符合要求:
func getMyString(items Items) (string, error) {
var buffer bytes.Buffer
var err error
var b []byte
for _, item := range items {
b, err = json.Marshal(item)
if err != nil {
return "", err
}
// use space to separate each json string in the array
buffer.WriteString(string(b) + " ")
}
s := strings.TrimSpace(buffer.String())
return s, nil
}