这是我的json字符串
var jsonBytes = "\"[{\"Fld1\":10,\"Fld2\":\"0.2\"},{\"Fld1\":10,\"Fld2\":\"0.26
\"}]\""
该字符串已转义双引号。如果我通过转换为[] bytes将其解组,则它无法正常工作,因为经过转换的[] byte数组仍然具有前导和尾随双引号。
如何删除go中的前导和尾随报价?
\n
jsonBytes。删除附近"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)
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句