从字符串golang中删除转义的双引号

问题描述 投票:0回答:1

这是我的json字符串

var jsonBytes = "\"[{\"Fld1\":10,\"Fld2\":\"0.2\"},{\"Fld1\":10,\"Fld2\":\"0.26
\"}]\""

此字符串已转义双引号。如果我通过转换为[]字节解组它,它不起作用,因为convereted []字节数组仍然具有前导和尾随双引号。

如何在go中删除那些前导和尾随引号?

go
1个回答
0
投票
  • 第一个错误是你在jsonBytes中输入了\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)
}
© www.soinside.com 2019 - 2024. All rights reserved.