如何在golang中将动态生成的数组对象数据转换为JSON格式的字符串?
发布于 2021-02-01 10:35:51
在数据检索中,数据采用数组对象的形式,如下所示:
[{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.}]
如何在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."}]
我尝试过的代码
结构
type Item struct {
Id int `json:"id"`
Category string `json:"category"`
Name string `json:"name"`
Description string `json:"description"`
}
type Items []Item
这里的数据检索功能
func GetData(productQuery interface{}) (result Items, err error) {
mongoSession := ConnectDb()
sessionCopy := mongoSession.Copy()
defer sessionCopy.Close()
getCollection := mongoSession.DB("custom").C("custom")
err = getCollection.Find(productQuery).Select(bson.M{"password": 0}).All(&result) //.Skip(skip).Limit(limit)
if err != nil {
return result, err
}
return result, nil
}
/*
*
* Retrieve the data used by main function
*
*
*/
func retrieve(c *gin.Context) {
conditions := bson.M{}
data, err :=GetData(conditions)
if err != nil {
fmt.Println("There is somthing wrong")
}
fmt.Println("--------------------")
fmt.Println(data)
fmt.Println("--------------------")
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))
}
我想使输出像
{"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
被浏览
660
1 个回答
-
该代码将起作用
package main import ( "bytes" "encoding/json" "fmt" "log" "strings" ) type Item struct { Id int `json:"id"` Category string `json:"category"` Name string `json:"name"` Description string `json:"description"` } type Items []Item var myJson = []byte(`[{ "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 main() { var items Items err := json.Unmarshal(myJson, &items) if err != nil { log.Fatal(err) } s, err := getMyString(items) if err != nil { log.Fatal(err) } fmt.Println(s) } 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()) return s, nil }
推荐阅读
-
如何在PHP中将字符串转换为JSON对象
2021-01-31 关注 0 浏览102 1答案
-
如何在Java中将对象数组转换为字符串数组
2022-05-26 关注 0 浏览22 1答案
-
如何在Java中将对象数组转换为字符串数组
2021-02-02 关注 0 浏览148 1答案
-
PowerShell:如何在 PowerShell 中将数组对象转换为字符串?
2022-07-28 关注 0 浏览25 1答案
-
如何在PHP中将字符串转换为数组
2021-02-02 关注 0 浏览172 1答案
-
如何在Golang中将类型从字符串转换为float64解码JSON?
2021-02-01 关注 0 浏览256 1答案
-
如何在Python中将JSON字符串转换为Avro?
2021-01-29 关注 0 浏览97 1答案
-
如何在C#中将字典转换为JSON字符串?
2021-02-02 关注 0 浏览153 1答案
-
如何在Java中将日期字符串转换为正确的格式?
2021-01-29 关注 0 浏览106 1答案
-
如何在Java中将字符串转换为Time对象?
2021-01-29 关注 0 浏览107 1答案