如何在golang中将动态生成的数组对象数据转换为JSON格式的字符串?

在数据检索中,数据采用数组对象的形式,如下所示:

[{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."}

有人可以帮我吗,我尝试了很多次,但是没有成功。

回答:

该代码将起作用

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

}

以上是 如何在golang中将动态生成的数组对象数据转换为JSON格式的字符串? 的全部内容, 来源链接: utcz.com/qa/422685.html

回到顶部