Go escape comma in JSON

huangapple go评论73阅读模式
英文:

Go escape comma in JSON

问题

如何在Go语言中的JSON标签中转义逗号?

type AA struct {
  MyStr string `json:"sub,ject"`
}

func main() {
  jsonStr := `
  {
    "sub,ject":         "I"
  }
  `
  stt := new(AA)
  json.Unmarshal([]byte(jsonStr), stt)
  fmt.Println(stt)

  t, _ := strconv.Unquote(jsonStr)
  fmt.Println(t)
}

这段代码无法正确解析键,并且只返回空结果。

如何转义逗号?

英文:

http://play.golang.org/p/lF2ZgAyxei

How do I escape comma in JSON tag in Go?

type AA struct {
  MyStr string `json:"sub,ject"`
}

func main() {
  jsonStr := `
  {
    "sub,ject":         "I"
  }
  `
  stt := new(AA)
  json.Unmarshal([]byte(jsonStr), stt)
  fmt.Println(stt)

  t, _ := strconv.Unquote(jsonStr)
  fmt.Println(t)
}

This does not grab the key and only returns empty results.

How do I escape the comma?

答案1

得分: 7

JSON编码包用于解析字段标签的代码位于tags.go中。该代码将字段名称与选项在第一个逗号处分隔开来。无法对逗号进行转义。

英文:

The code used by the JSON encoding package to parse field tags is in tags.go. This code splits the field name from the options at the first comma. It not possible to escape the comma.

huangapple
  • 本文由 发表于 2014年9月23日 07:19:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/25984521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定