如何在golang中将字符串值作为json对象返回?

时间:2022-12-10 07:08:09

I am using golang with beego framework and I have problem with serving strings as json.

我使用gogo与beego框架,我有服务字符串作为json的问题。

EventsByTimeRange returns a string value in json format

EventsByTimeRange以json格式返回字符串值

this.Data["json"] = dao.EventsByTimeRange(request) // this -> beego controller
this.ServeJson()

"{\"key1\":0,\"key2\":0}"

How can I get rid of quotation marks?

我怎样才能摆脱引号?

2 个解决方案

#1


4  

you can re-define your json format string in a new type. this is a small demo

您可以在新类型中重新定义json格式字符串。这是一个小型演示

package main

import (
    "encoding/json"
    "fmt"
)

type JSONString string

func (j JSONString) MarshalJSON() ([]byte, error) {
    return []byte(j), nil
}

func main() {
    s := `{"key1":0,"key2":0}`
    content, _ := json.Marshal(JSONString(s))
    fmt.Println(_, string(content))
}   

in your case you can write like this

在你的情况下,你可以像这样写

this.Data["json"] = JSONString(dao.EventsByTimeRange(request))
this.ServeJson()   

BTW,golang-json package adds quotation marks because it treats your string as a json value,not a json k-v object.

BTW,golang-json包添加了引号,因为它将您的字符串视为json值,而不是json k-v对象。

#2


1  

The string you got is a fine JSON formatted value. all you need is to unmarshal it into a correct type.

你得到的字符串是一个很好的JSON格式值。您所需要的只是将其解组为正确的类型。

See below code.

见下面的代码。

However, I think you misunderstood the ServeJson(), it returns a JSON formatted string which your client will use it, and it does that just fine (see your question).

但是,我认为你误解了ServeJson(),它返回一个JSON格式的字符串,你的客户端将使用它,并且它做得很好(参见你的问题)。

If you remove the qoutes and slashes, You'll end up with invalid JSON string!

如果你删除qoutes和斜杠,你最终会得到无效的JSON字符串!

package main

import "fmt"
import "log"
import "encoding/json"
func main() {
    var b map[string]int
    err := json.Unmarshal ([]byte("{\"key1\":0,\"key2\":0}"), &b)
    if err != nil{
        fmt.Println("error: ", err)
    }
    log.Print(b)
    log.Print(b["key1"])
}

You'll get: map[key1:0 key2:0]

你会得到:map [key1:0 key2:0]

#1


4  

you can re-define your json format string in a new type. this is a small demo

您可以在新类型中重新定义json格式字符串。这是一个小型演示

package main

import (
    "encoding/json"
    "fmt"
)

type JSONString string

func (j JSONString) MarshalJSON() ([]byte, error) {
    return []byte(j), nil
}

func main() {
    s := `{"key1":0,"key2":0}`
    content, _ := json.Marshal(JSONString(s))
    fmt.Println(_, string(content))
}   

in your case you can write like this

在你的情况下,你可以像这样写

this.Data["json"] = JSONString(dao.EventsByTimeRange(request))
this.ServeJson()   

BTW,golang-json package adds quotation marks because it treats your string as a json value,not a json k-v object.

BTW,golang-json包添加了引号,因为它将您的字符串视为json值,而不是json k-v对象。

#2


1  

The string you got is a fine JSON formatted value. all you need is to unmarshal it into a correct type.

你得到的字符串是一个很好的JSON格式值。您所需要的只是将其解组为正确的类型。

See below code.

见下面的代码。

However, I think you misunderstood the ServeJson(), it returns a JSON formatted string which your client will use it, and it does that just fine (see your question).

但是,我认为你误解了ServeJson(),它返回一个JSON格式的字符串,你的客户端将使用它,并且它做得很好(参见你的问题)。

If you remove the qoutes and slashes, You'll end up with invalid JSON string!

如果你删除qoutes和斜杠,你最终会得到无效的JSON字符串!

package main

import "fmt"
import "log"
import "encoding/json"
func main() {
    var b map[string]int
    err := json.Unmarshal ([]byte("{\"key1\":0,\"key2\":0}"), &b)
    if err != nil{
        fmt.Println("error: ", err)
    }
    log.Print(b)
    log.Print(b["key1"])
}

You'll get: map[key1:0 key2:0]

你会得到:map [key1:0 key2:0]