json - 从types.JSON.TEXT转换为字符串列表

标签 json string go text trim

我有一个JSON.TEXT(https://godoc.org/github.com/jmoiron/sqlx/types#JSONText),我需要将其转换为字符串列表。例如,如何转换

"[equals, less than]" // JSON.TEXT type
["equals", "less than"] // list of strings
我试图通过使用string(),修剪[]括号并加入它们来显式转换JSON.TEXT类型,但是最后得到的是带有反斜杠的字符串数组:
["\"equal to\"", " \"less than equal to\""]
还有另一种方法可以实现此目的,或者如何摆脱反斜杠?

最佳答案

从修剪/分割的结果来看,您拥有的变量看起来是

types.JSONText([]byte(`["equals", "less than"]`))
要将其转换为字符串 slice ,可以使用json.Unmarshal函数
package main

import (
    "encoding/json"
    "fmt"

    "github.com/jmoiron/sqlx/types"
)

func main() {
    txt := types.JSONText([]byte(`["equals", "less than"]`))
    var slice []string
    json.Unmarshal(txt, &slice)
    fmt.Println(slice)
}
这是操场的链接:https://play.golang.com/p/cAiJjWNu8Vx

关于json - 从types.JSON.TEXT转换为字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62568701/

相关文章:

php - 在 Get_Where 查询中匹配 JSON 字段(Code Igniter)

json - 如何从 json 数组获取最新日期键值对(包括父键)

c# - 从 JSON 字符串中删除空数组成员

c++ - BSTR 到 std::string (std::wstring),反之亦然

rest - Golang gorilla mux REST api 在使用 PUT 和 DELETE 方法时出现 405 错误

java - 如何验证传入的 JSON 并检查缺少的属性? [ jackson , Jersey ]

c++ - 为函数提供字符串或整数

java - 在 Java 中将 File[] 转换为 String[]

go - 将字符串(键)映射到数字(或任何数据)

Go using mux Router - 如何将我的数据库传递给我的处理程序