英文:
Go - How to deal with JSON response that has attribute that can be of different types
问题
假设我有以下结构体:
type Response struct {
ID string `json:"id"`
Edited interface{} `json:"edited"`
}
type Responses []Response
然后假设我向一个API发送请求,但是API文档和测试告诉我edited
字段的值可以是bool
或int
类型。这显然会导致Go语言出错,因为它无法将响应体解码到结构体中。
// ... http GET
// response [{"edited": true, "id": 1}, {"edited": 1683248234.0, "id": 2}]
r := Responses{}
err := json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
在无法自动加载到结构体中的情况下,我该如何处理上述情况?我猜想我需要先将其加载到一个接口中,然后过滤切片并处理各自不同的Response类型的结构体?但是然后我无法将它们合并!
因此,我考虑将字段转换为bool
或int
类型之一。
注:这与Reddit API相关,其中一些字段(如edited
、created
、created_utc
)没有符合规范的类型。
英文:
Let's say I have the following struct
type Response struct {
ID string `json:"id"`
Edited int `json:"edited"`
}
type Responses []Response
Then lets say I send a request to an API, but my issue is the API docs and from testing have told me that the edited
value can come back as a bool
or as a int
. This obviously upsets Go and it throws an error when decoding the response body into the struct.
// ... http GET
// response [{"edited": true, id: 1}, {"edited": 1683248234.0, id: 2}]
r := Responses{}
err := json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
How can I handle the above situation where I can't automatically load it into a struct? I'm assuming I'd need to do it into an interface first then filter the slice and handle the two different Response types in their own structs? But then I can't combine them!
So I'm thinking of conforming the field to one or the other, bool or int.
n.b. this relates to the Reddit API where some of the fields such as edited
, created
, created_utc
don't have conforming types.
答案1
得分: 3
根据@mkopriva
的建议,处理不同类型的变量的最简单方法是使用interface{}
类型:
const resp = `[{"edited": true, "id": 1}, {"edited": 1683248234.0, "id": 2}, {"id": 3}]`
type Response struct {
ID int `json:"id"`
Edited interface{} `json:"edited"`
}
type Responses []Response
r := Responses{}
err := json.Unmarshal([]byte(resp), &r)
if err != nil {
log.Fatal(err)
}
for _, response := range r {
switch response.Edited.(type) {
case float64:
fmt.Println("float64")
case bool:
fmt.Println("bool")
default:
fmt.Println("invalid")
}
}
另外,你可以定义一个具有自定义json.Unmarshaler
实现的新类型:
type Response struct {
ID int `json:"id"`
Edited Edited `json:"edited"`
}
type Edited struct {
BoolVal *bool
FloatVal *float64
}
func (e *Edited) UnmarshalJSON(data []byte) error {
boolVal, err := strconv.ParseBool(string(data))
if err == nil {
e.BoolVal = &boolVal
return nil
}
floatVal, err := strconv.ParseFloat(string(data), 64)
if err == nil {
e.FloatVal = &floatVal
return nil
}
return errors.New("undefined type")
}
r := Responses{}
err := json.Unmarshal([]byte(resp), &r)
if err != nil {
log.Fatal(err)
}
for _, response := range r {
edited := response.Edited
switch {
case edited.FloatVal != nil:
fmt.Println("float64")
case edited.BoolVal != nil:
fmt.Println("bool")
default:
fmt.Println("invalid")
}
}
英文:
As told @mkopriva
, the simplest way to handle different type of variable is use interface{}
type:
const resp = `[{"edited": true, "id": 1}, {"edited": 1683248234.0, "id": 2}, {"id": 3}]`
type Response struct {
ID int `json:"id"`
Edited interface{} `json:"edited"`
}
type Responses []Response
r := Responses{}
err := json.Unmarshal([]byte(resp), &r)
if err != nil {
log.Fatal(err)
}
for _, response := range r {
switch response.Edited.(type) {
case float64:
fmt.Println("float64")
case bool:
fmt.Println("bool")
default:
fmt.Println("invalid")
}
}
<kbd>PLAYGROUND</kbd>
Also you can define new type
with custom json.Unmarshaler
implementation:
type Response struct {
ID int `json:"id"`
Edited Edited `json:"edited"`
}
type Edited struct {
BoolVal *bool
FloatVal *float64
}
func (e *Edited) UnmarshalJSON(data []byte) error {
boolVal, err := strconv.ParseBool(string(data))
if err == nil {
e.BoolVal = &boolVal
return nil
}
floatVal, err := strconv.ParseFloat(string(data), 64)
if err == nil {
e.FloatVal = &floatVal
return nil
}
return errors.New("undefined type")
}
r := Responses{}
err := json.Unmarshal([]byte(resp), &r)
if err != nil {
log.Fatal(err)
}
for _, response := range r {
edited := response.Edited
switch {
case edited.FloatVal != nil:
fmt.Println("float64")
case edited.BoolVal != nil:
fmt.Println("bool")
default:
fmt.Println("invalid")
}
}
<kbd>PLAYGROUND</kbd>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论