Golang Go语言中关于json化的问题,重写 UnmarshalJSON 后取不到值了
Golang Go语言中关于json化的问题,重写 UnmarshalJSON 后取不到值了
关于 json 化,定义了 IdArr 的结构体,主要目的是为了接收前端字符数组,自动转换为数值型; 在嵌套到 A 中时,可以取到 ids 的值,more 的值永远为空.
package main
import (
"encoding/json"
"fmt"
"strconv"
)
type IdArr struct {
Ids []uint64 `json:"ids"`
}
type A struct {
IdArr
More string `json:"more"`
}
func (s *IdArr) UnmarshalJSON(data []byte) error {
type Temp IdArr
t := struct {
Ids []string `json:"ids"`
*Temp
}{
Temp: (*Temp)(s),
}
if err := json.Unmarshal(data, &t); err != nil {
return err
}
for _, id := range t.Ids {
uId, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return err
}
s.Ids = append(s.Ids, uint64(uId))
}
return nil
}
func main() {
d := `
{"ids":["1213334"], "more": "text"}
`
a := &A{}
json.Unmarshal([]byte(d), &a)
fmt.Printf("%+v", a)
}
输出
&{IdArr:{Ids:[1213334]} More:}
https://play.golang.org/p/mjR2TrSfbh7
卡了半天了,请问什么原因呢?
更多关于Golang Go语言中关于json化的问题,重写 UnmarshalJSON 后取不到值了的实战教程也可以访问 https://www.itying.com/category-94-b0.html
unmarshalJSON 通篇和 struct A 没有任何关系,代码写错了吧
更多关于Golang Go语言中关于json化的问题,重写 UnmarshalJSON 后取不到值了的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
去除 unmarshalJSON 之后 A 就可以正常转换了,所以怀疑是重写导致了什么奇怪的 bug
https://stackoverflow.com/questions/53228912/unmarshal-field-of-nested-struct-not-work
这个回答应该能够解决的问题,引用一下
> The calling of ‘json.Unmarshal’ on ‘Bar’ will actually do the following things:
1. calling UnmarshalJSON on Bar.
2. Because anonymous struct in Bar don’t implemented UnmarshalJSON, so calling UnmarshalJSON on it’s embedding struct Foo instead.
That’s why ‘Entry’ in anonymous struct will not be unmarshal.
多谢,只能换个写法了, 我直觉理解成他递归调用序列化方法了
简单的跟了一下 unmarshal,在 1.14.4 版本情况下:
encoding/json/decode.go:620 行开始
1. 622 行的 indirect 函数开始找实现了 unmarshaler 的结构,对应你的代码就是找到了 IdArr,然后停止
2. 然后进入了 623 行的判断,读到本层 json 结构开始的数组下标
3. 注意,624 行的 d.skip() 函数,将当前读取[]byte 的下标跳到了本层 json 结构结束的地方,对应楼主的代码就是跳到了这个 json 文本结束的位置
4. 跳出后由于 d (记录解析状态的结构体)保存的读取下标已经到了 json 文本的结束,所以结束了 unmarshal
json 库代码在实现了 unmarshaler 的情况下 unmarshal 逻辑:unmarshal 时扫描 json 文本,由于实现了 unmarshaler 接口,在库代码里,作者应该是默认 unmarshaler 会完整的自行反序列化对应的 struct,所以在库代码中这里在检测到实现了 unmarshaler 接口后,下次开始的下标就直接跳过了本层 json 结构(跳过目前开始位置的 depth 后的结构),交由 unmarshaler 实现的接口去实现。
所以楼主的代码的问题就是:ids 和 more 是一个层级的,由于实现了 unmarshaler,unmarshal 时就将 ids 和 more 这同一层级的数据[]byte 都交由 IdArr 的 unmarshal 方法去进行,但是楼主实现的 unmarshal 只反序列化了 ids 而没有去实现 more,在跳出后,由于扫描的下标已经跳到了 json 文本的结尾,所以直接结束了 unmarshal 。
归根到底就是库代码作者觉得你实现了某个 unmarshaler 就把这一 depth 的都交给你去自行实现然后跳过这一 depth,而作者只是实现了部分数据的 unmarshal,所以导致了问题。
非常详细了,多谢多谢, 我换成一层,非嵌套就 OK 了😄
本意是想匿名组合复用 idArr,库这么设计只能不搞嵌套了
A 继承了 IdArr 的方法,也就是 A 也实现了 UnmarshalJSON 方法
在Golang中,当你重写结构体的 UnmarshalJSON
方法时,需要确保你正确地解析了JSON数据,并将其赋值给了结构体的字段。如果在重写 UnmarshalJSON
后取不到值,可能是以下几个原因:
-
解析错误:检查你的
UnmarshalJSON
实现中是否有解析错误,确保使用了正确的JSON解析方法,如json.Unmarshal
,并且正确处理了可能返回的错误。 -
字段赋值遗漏:确保在解析JSON后,将解析出的值正确地赋值给了结构体的字段。如果忘记这一步,即使解析成功,结构体的字段也不会被更新。
-
类型不匹配:确保JSON数据的类型与你期望解析到的Go类型匹配。类型不匹配会导致解析失败或值不正确。
-
指针问题:如果你的结构体字段是指针类型,确保在解析后正确地分配了内存,并将解析出的值赋给了指针指向的位置。
-
嵌套结构体:如果结构体包含嵌套结构体,确保嵌套结构体的
UnmarshalJSON
方法(如果重写了)也被正确调用,并且嵌套结构体的字段也被正确赋值。
解决这类问题的一个好方法是添加日志打印,在 UnmarshalJSON
方法的不同步骤中打印出相关变量的值,这样可以帮助你定位问题所在。同时,确保你的测试覆盖了不同的JSON输入场景,以验证 UnmarshalJSON
方法的正确性和健壮性。