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 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀
  • ¥50 我在一个购物网站的排队系统排队,这个排队到号后重新定向到目标网站进行购物,但是有技术牛通过技术方法直接跳过排队系统进入目标网址购物,有没有什么软件或者脚本可以用