这是我的json字符串
var jsonBytes = "\"[{\"Fld1\":10,\"Fld2\":\"0.2\"},{\"Fld1\":10,\"Fld2\":\"0.26
\"}]\""
该字符串已转义了双引号。如果我通过转换为[] bytes来对进行解组,则它不起作用,因为经过转换的[] byte数组仍带有前导和尾随双引号。
如何删除go中的前导和尾随报价?
最佳答案
\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)
}