Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go escape comma in JSON

Tags:

json

go

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 Answers

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.

like image 184
Simon Fox Avatar answered Nov 09 '25 09:11

Simon Fox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!