dongxiao_0528 2019-07-10 20:31
浏览 783

从编组的结构Golang中删除转义字符

The format of my JSON output after I marshal a struct has a lot of escape characters and double quotes.

I've tried using an encoder, Marshalling, RawMessages, forcefully removing parts of the string.

data := ChannelData{}
if err := rows.Scan(&data.Idx, &data.MciIdx, &data.Channel, &data.MatchIdx, &data.MatchCx, &data.StartTs, &data.EndTs, &data.Len, &data.MatchStartTs, &data.MatchEndTs, &data.MatchLen, &data.Happened, &data.Instance); err != nil {
log.Printf("%+v
", rows)
log.Error().Err(err).Msg("unable to scan mysql data into struct")
continue
}

jsonD, err := json.Marshal(&data)
if err != nil {
log.Error().Err(err).Msg("cannot marshal data")
}
log.Debug().Msg(string(jsonD))

type ChannelData struct {
    Idx          string `json:"idx,string"`
    MciIdx       string `json:"mci_idx,string"`
    Channel      string `json:"channel,string"`
    MatchIdx     string `json:"match_idx,string"`
    MatchCx      string `json:"match_cx,string"`
    StartTs      string `json:"start_ts,string"`
    EndTs        string `json:"end_ts,string"`
    Len          string `json:"len,string"`
    MatchStartTs string `json:"match_start_ts,string"`
    MatchEndTs   string `json:"match_end_ts,string"`
    MatchLen     string `json:"match_len,string"`
    Happened     string `json:"happened,string"`
    Instance     string `json:"instance,string"`
}

I am getting:

{"level":"debug","time":"2019-07-10T20:12:09Z","message":"{\"idx\":\"\\"8931741865\\"\",\"mci_idx\":\"\\"107265043\\"\",\"channel\":\"\\"WPVIDT\\"\",\"match_idx\":\"\\"36028797060807935\\"\",\"match_cx\":\"\\"\\"\",\"start_ts\":\"\\"2019-07-10 17:57:59\\"\",\"end_ts\":\"\\"2019-07-10 17:58:14\\"\",\"len\":\"\\"00:00:15\\"\",\"match_start_ts\":\"\\"2019-06-05 07:14:52\\"\",\"match_end_ts\":\"\\"2019-06-05 07:15:08\\"\",\"match_len\":\"\\"00:00:16\\"\",\"happened\":\"\\"2019-07-10 17:58:16\\"\",\"instance\":\"\\"172.17.65.80\\"\"}"}

I expect it to be in JSON format:

{"level":"debug","time":"2019-07-10T20:12:09Z","message":"{"idx":"8931741865","mci_idx":"107265043","channel":"WPVIDT"...}"}

  • 写回答

1条回答 默认 最新

  • douou6807 2019-07-10 21:00
    关注

    For removing double quotes you could use strconc.Unquote

    It would be great to see an example of a row of data to see if anything else is missing.

    But from what is available here, if there is quotes in the string json.Marshal will not automagically remove them. You can remove quotes using the above command.

    Look at This Example for Unquote.

    You can probably just write one method on the ChannelData struct that will unquote each field.

    func (c *ChannelData) unquote() {
      c.Idx = strconv.Unquote(c.Idx)
    .
    .
    .
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug