dongyuan7981 2017-12-31 04:22
浏览 3105
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dsgm5631 2017-12-31 07:14
    关注
    • First mistake is you have enter in your jsonBytes. remove that near "0.26
    • you have \ 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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失