简体   繁体   English

没有转义字符的 Go(Gin) 中的 Json 响应

[英]Json response in Go(Gin) without escape characters

I have recently started working on Go APIs using GIN.我最近开始使用 GIN 研究 Go API。 My API is getting the data from DB with two columns where one column contains integer and other contains a json string.我的 API 正在从具有两列的 DB 中获取数据,其中一列包含 integer,另一列包含 json 字符串。 The json string is dynamic and hence i cant use struct for that. json 字符串是动态的,因此我不能为此使用结构。 I am using map[string]interface{} to parse the json and modify it and then parse it back to json using json.Marshal .我正在使用map[string]interface{}解析 json 并对其进行修改,然后使用 json.Marshal 将其解析回json.Marshal Now i am returning this json string as a response but getting escape characters.现在我返回这个 json 字符串作为响应,但得到转义字符。 Have done some search regarding that, but didnt find any solution yet.对此进行了一些搜索,但尚未找到任何解决方案。 Here is the part of code that i am using这是我正在使用的部分代码

var interface_obj map[string]interface{}
json.Unmarshal([]byte(grants.Data), &interface_obj)
grants_map := interface_obj["role_grants"].(map[string]interface{})
jsonString, err := json.Marshal(grants_map)
jsonBody := string(jsonString)

After this, I am returning this JSON as response in GIN framework like this在此之后,我将返回这个 JSON 作为 GIN 框架中的响应,如下所示

c.JSON(http.StatusCreated, gin.H{"message": "Json retrieved successfully", "data": jsonBody})

But the output i am getting is但是我得到的 output 是

{
    "data": "[{\"action\":\"read\",\"resource\":\"project\"},{\"action\":\"all\",\"resource\":\"users\"},{\"action\":\"all\",\"resource\":\"roles\"},{\"action\":\"all\",\"resource\":\"project-settings\"},{\"action\":\"create\",\"resource\":\"single-entity-screening\"},{\"action\":\"read\",\"resource\":\"single-entity-screening\"},{\"action\":\"create\",\"resource\":\"multi-batch-screening\"},{\"action\":\"read\",\"resource\":\"multi-batch-screening\"},{\"action\":\"read\",\"resource\":\"workspace\"},{\"action\":\"allocate\",\"resource\":\"workspace\"},{\"action\":\"update\",\"resource\":\"workspace\"},{\"action\":\"read\",\"resource\":\"case\"},{\"action\":\"allocate\",\"resource\":\"case\"},{\"action\":\"review\",\"resource\":\"case\"},{\"action\":\"update\",\"resource\":\"case\"},{\"action\":\"read\",\"resource\":\"report\"},{\"action\":\"read\",\"resource\":\"audit-trail\"},{\"action\":\"read\",\"resource\":\"delivery\"}]",
    "message": "Grants retrieved successfully"
}

I printed it on my console and it looked fine there, but causing this issue on response.我将它打印在我的控制台上,它在那里看起来很好,但是在响应时导致了这个问题。 Is there any way to resolve this using some standard way?有什么方法可以使用一些标准方法来解决这个问题吗? Please guide Thanks请指导 谢谢

You don't need to do json.Marshal(grants_map) , just pass the value directly to gin.H and let c.JSON do the encoding, ie您不需要执行json.Marshal(grants_map) ,只需将值直接传递给gin.H并让c.JSON进行编码,即

gin.H{... "data": grants_map}

And in cases where you truly have raw JSON data at hand that you want to send as part of other not-yet-JSON data, you can wrap it into json.RawMessage to avoid the "double-encoding", ie如果您手头确实有原始 JSON 数据想要作为其他尚未 JSON 数据的一部分发送,您可以将其包装到json.RawMessage以避免“双重编码”,即

gin.H{... "data": json.RawMessage(jsonBody)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM