Skip to content

Commit 794419f

Browse files
authored
Update go-tips.cn.md
1 parent 65528e9 commit 794419f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

go-tips.cn.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ type TestObject struct {
131131
如果json是 `{"Field1": 100}` 则会报错
132132

133133
# 容忍字符串和数字互转
134+
135+
如果你使用的是jsoniter,可以启动模糊模式来支持 PHP 传递过来的 JSON。
136+
137+
```golang
138+
import "github.com/json-iterator/go/extra"
139+
140+
extra.RegisterFuzzyDecoders()
141+
```
142+
143+
这样就可以处理字符串和数字类型不对的问题了。比如
144+
145+
```golang
146+
var val string
147+
jsoniter.UnmarshalFromString(`100`, &val)
148+
```
149+
150+
又比如
151+
152+
```golang
153+
var val float32
154+
jsoniter.UnmarshalFromString(`"1.23"`, &val)
155+
```
156+
134157
# 容忍空数组作为对象
135158
# 使用 MarshalJSON支持time.Time
136159
# 使用 RegisterTypeEncoder支持time.Time

0 commit comments

Comments
 (0)