劲创营---任务项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

39 lines
773 B

  1. package utils
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "regexp"
  6. )
  7. func JsonMarshal(interface{}) {
  8. }
  9. // 不科学计数法
  10. func JsonDecode(data []byte, v interface{}) error {
  11. d := json.NewDecoder(bytes.NewReader(data))
  12. d.UseNumber()
  13. return d.Decode(v)
  14. }
  15. // json字符串驼峰命名格式 转为 下划线命名格式
  16. //
  17. // c :json字符串
  18. func MarshalJSONCamelCase2JsonSnakeCase(c string) []byte {
  19. // Regexp definitions
  20. var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
  21. var wordBarrierRegex = regexp.MustCompile(`(\w)([A-Z])`)
  22. marshalled := []byte(c)
  23. converted := keyMatchRegex.ReplaceAllFunc(
  24. marshalled,
  25. func(match []byte) []byte {
  26. return bytes.ToLower(wordBarrierRegex.ReplaceAll(
  27. match,
  28. []byte(`${1}_${2}`),
  29. ))
  30. },
  31. )
  32. return converted
  33. }