猿问

我怎样才能从带有反斜杠的字符串中形成一个漂亮的 JSON?

我在Golang中有一个Gin-Gonic REST API。在这里我试图输出已经注册为 JSON 的用户,目前在 Postman 中我只得到:


(您可以忽略lastRequest-Attribut,因为它目前始终为 nil)


"[{\"id\":\"e61b3ff8-6cdf-4b23-97a5-a28107c57543\",\"firstname\":\"John\",\"lastname\":\"Doe\",\"email\":\"john@doe.com\",\"username\":\"johndoe\",\"token\":\"19b33c79-32cc-4063-9381-f2b64161ad8a\",\"lastRequest\":null},

但我想要这样:


[{

        "id": "e61b3ff8-6cdf-4b23-97a5-a28107c57543",

        "username": "johndoe",

        "email": "john@doe.com",

        "token": "19b33c79-32cc-4063-9381-f2b64161ad8a",

        "lastRequest": null

    }]

我该如何管理它,我用“ json.MarshalIndent ”尝试了很多东西(来自这个stackoverflow-post),但是它对我没有任何改变,我需要做什么?因为无论我做什么,反斜杠都会保留,最多\t插入空格。我还读到我必须将它作为一个字节数组来做,但这对我也不起作用(也许我在这里也做错了)。


这是我当前的代码:

var users []User

r.GET("/getusers", func(c *gin.Context) {

    usersArr := make([]User, len(users))

    for i := 0; i < len(users); i++ {

        usersArr[i] = users[i]

    }

        

    userJson, err := json.Marshal(testUser)

    if err != nil {

        c.JSON(400, gin.H{"error": err.Error()})

    } else {

        c.JSON(200, string(userJson))

    }

})


type User struct {

    Id          string        `json:"id"`

    Firstname   string        `json:"firstname"`

    Lastname    string        `json:"lastname"`

    Email       string        `json:"email"`

    Username    string        `json:"username"`

    Token       string        `json:"token"`

    LastRequest []lastRequest `json:"lastRequest"`

}


Smart猫小萌
浏览 252回答 3
3回答

米脂

两件事情:“反斜杠”只是转义字符。他们实际上并不在那里。缩进 JSON 很简单,只需调用适当的 Indent 函数即可:如果您已经有了 JSON 字符串,请使用包中的json.Indent函数encoding/json:input := []byte("[{\"id\":\"e61b3ff8-6cdf-4b23-97a5-a28107c57543\",\"firstname\":\"John\",\"lastname\":\"Doe\",\"email\":\"john@doe.com\",\"username\":\"johndoe\",\"token\":\"19b33c79-32cc-4063-9381-f2b64161ad8a\",\"lastRequest\":null}]")buf := &bytes.Buffer{}if err := json.Indent(buf, input, "", "\t"); err != nil {&nbsp; &nbsp; panic(err)}fmt.Println(buf.String())游乐场链接但是,如果您试图直接编组为缩进形式,只需使用MarshalIndent函数 intead of Marshal:&nbsp; &nbsp; userJson, err := json.MarshalIndent(testUser, "", "\t")

达令说

好吧,这将是我在 stackoverflow 中的第一个答案,希望这会有所帮助。Go gin 框架带有一些方便的功能,因为我不知道你使用的是什么版本的 golang 和 Gin 框架,你可以试试这个:var users []Userr.GET("/getusers", func(c *gin.Context) {&nbsp; &nbsp; usersArr := make([]User, len(users))&nbsp; &nbsp; for i := 0; i < len(users); i++ {&nbsp; &nbsp; &nbsp; &nbsp; usersArr[i] = users[i]&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; c.JSON(400, gin.H{"error": err.Error()})&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; c.JSON(200, users)// If Indentation is required you could try:&nbsp; &nbsp; &nbsp; &nbsp; c.IndentedJSON(200, users)&nbsp; &nbsp; }})type User struct {&nbsp; &nbsp; Id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; `json:"id"`&nbsp; &nbsp; Firstname&nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"firstname"`&nbsp; &nbsp; Lastname&nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; `json:"lastname"`&nbsp; &nbsp; Email&nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"email"`&nbsp; &nbsp; Username&nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; `json:"username"`&nbsp; &nbsp; Token&nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"token"`&nbsp; &nbsp; LastRequest []lastRequest `json:"lastRequest"`}

泛舟湖上清波郎朗

希望这个功能有所帮助。import ("encoding/json""strings")func logRequest(log *logrus.Entry, data []byte) {&nbsp; &nbsp; &nbsp; &nbsp; cp := bytes.NewBuffer([]byte{})&nbsp; &nbsp; &nbsp; &nbsp; if err := json.Compact(cp, data); err == nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Info("Request received: ", string(cp.Bytes()))&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringifiedData := strings.Replace(string(data[:]), "\n", "", -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringifiedData = strings.Replace(stringifiedData, "\r\n", "", -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringifiedData = strings.Replace(stringifiedData, "\t", "", -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Info("Request received: ", strings.ReplaceAll(stringifiedData, " ", ""))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Go
我要回答