从mongodb中检索到一个数组对象。 数据如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [{ 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. }] |
这是用于检索以上数据的代码
结构为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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)) } |
运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [{ "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." }] |
预期产量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | { "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." } |
问题是数据在数组对象中供我使用,如上所述,我需要
- 嗨@Puneet,您的预期输出是多少? 而且,从mongodb检索到的数据对您来说对我来说似乎无效。
- @srf然后我应该怎么做才能帮助我成为golang的新手。 你能帮助我吗?
- 我会尽力的,您能描述一下您的用例吗?
- 如何在golang中将动态生成的数组对象数据转换为JSON格式的可能重复项?
- 没有理由两次发表您的问题。 如果您想澄清一些问题,请编辑现有问题。
根据OP讨论的内容,编组struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [{ "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希望获得一个类似于以下内容的字符串:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | { "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." } |
我考虑了两种方法:
遍历项目并将每个封送处理的项目附加到字符串中(显然效率不高)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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 } |
或只是修剪前和后方括号:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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 } |
链接到新代码:https://play.golang.org/p/dVvsQlsRqZO
-
先生,每件事都很糟糕,但有一件事给我错误,字符串用逗号分隔了,否则会是一个空格。
} 惯用语后请参阅逗号