关于 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/mjR...
卡了半天了,请问什么原因呢?
主要原因还是没有理解golang里面的结构体匿名嵌套和继承的问题:
当IdArr匿名嵌套在A结构体内部时,IdArr的接口会被A继承
所以当你对IdArr进行重写方法UnmarshalJSON时,A就继承了UnmarshalJSON方法
所以在调用json.Unmarshal([]byte(d), &a)时,直接调用了A的UnmarshalJSON,其实就是新定义的UnmarshalJSON方法,
其入参是(转换为string后):{"ids":["1213334"], "more": "text"}
而你的UnmarshalJSON方法其实都是在处理IdArr结构,从而more这个定义被忽略了,导致解析不出来。
修改方法:
1、按照eudore给的最小粒度重写方法来做
2、不要重写IdArr的UnmarshalJSON,而是直接重写A的UnmarshalJSON方法:
(将如下方法替换掉你的UnmarshalJSON)
3、修改结构体的定义,不要匿名嵌套,就不会有继承的问题:
假如你的入参只能是{"ids":["1213334"], "more": "text"}这种结构的话,这种就你的初衷不符合,入参也同样的要改
补充一下,你在UnmarshalJSON中定义的Temp毫无意义。