这是我的json字符串
var jsonBytes = "\"[{\"Fld1\":10,\"Fld2\":\"0.2\"},{\"Fld1\":10,\"Fld2\":\"0.26
\"}]\""
此字符串已转义双引号。如果我解组它转换为[]字节,它不起作用,因为convereted []字节数组仍然有前导和尾随双引号。
如何删除go中的引号和尾随引号?
答案 0 :(得分:0)
\n
。删除"0.26
\
。我将在下面告诉您删除它:`
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"github.com/Gujarats/logger"
)
type Msg struct {
Channel int `json:"Fld1"`
Name string `json:"Fld2"`
Msg string
}
func main() {
var msg []Msg
var jsonBytes = "\"[{\"Fld1\":10,\"Fld2\":\"0.2\"},{\"Fld1\":10,\"Fld2\":\"0.26\"}]\""
// Removing the the first and the last '\'
newVal := jsonBytes[1 : len(jsonBytes)-1]
logger.Debug("newval type ", reflect.TypeOf(newVal))
logger.Debug("newval ", newVal)
err := json.Unmarshal([]byte(newVal), &msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", msg)
}