2 Star 0 Fork 0

403716045/gcore

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
JsonSnakeCase.go 664 Bytes
Copy Edit Raw Blame History
xingang authored 2023-02-17 18:19 +08:00 . init
package helper
import (
"encoding/json"
"regexp"
"unicode"
)
type JsonSnakeCase struct {
Value interface{}
}
func (c JsonSnakeCase) MarshalJSON() ([]byte, error) {
var keyMatchRegex = regexp.MustCompile(`\"(\w+)\":`)
marshalled, err := json.Marshal(c.Value)
converted := keyMatchRegex.ReplaceAllFunc(
marshalled,
func(match []byte) []byte {
matchStr := string(match)
key := matchStr[1 : len(matchStr)-2]
resKey := Lcfirst(key)
return []byte(`"` + resKey + `":`)
},
)
return converted, err
}
// 首字母小写
func Lcfirst(str string) string {
for i, v := range str {
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lv_baobao/gcore.git
git@gitee.com:lv_baobao/gcore.git
lv_baobao
gcore
gcore
3bf2efb9b087

Search