This my json string
var jsonBytes = "\"[{\"Fld1\":10,\"Fld2\":\"0.2\"},{\"Fld1\":10,\"Fld2\":\"0.26
\"}]\""
This string has escaped double quotes. If I Unmarshal it by converting to []bytes, its not working, as the convereted []byte array still has leading and trailing double quotes.
How do I remove those leading and trailing quotes in go?
in your jsonBytes. remove that near "0.26
\
in the first and the last data. I will show you to remove it below :`
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
", msg)
}