高浪如何接受不确定的价值观

后端返回值不是固定的,有时:


{"application": {"instance": [{"instanceId": "v1"}, {"instanceId": "v2"}]}}

或者有时:


{"application": {"instance": {"instanceId": "v"}}}

我应该如何取出相应的实例 Id 值?


package main


import (

    "encoding/json"

    "fmt"

)


type Application struct {

    Application struct {

        Instance json.RawMessage `json:"instance"`

    } `json:"application"`

}



func main() {

    a := `{"application": {"instance": {"instanceId": "v"}}}`

    //a := `{"application": {"instance": [{"instanceId": "v1"}, {"instanceId": "v2"}]}} `

    var p Application

    errJson := json.Unmarshal([]byte(a), &p)

    if errJson != nil {

        fmt.Printf("errJson")

    }

    fmt.Printf("type:%T", p.Application.Instance)


}


幕布斯6054654
浏览 67回答 2
2回答

幕布斯7119047

由于 2 种值类型发生冲突(一种是结构,另一种是结构的一部分),因此即使使用捕获所有解决方案(如 ),将其封装到单个类型中也会变得很混乱。interface{}最简单的解决方案是呈现两种不同的类型并封送到其中任何一个,以查看哪个“有效”:func unmarsh(body []byte) (*type1, *type2, error) {    var (        t1 type1        t2 type2    )    err := json.Unmarshal(body, &t1)    if err == nil {        return &t1, nil, nil    }    err = json.Unmarshal(body, &t2)    if err == nil {        return nil, &t2, nil    }    return nil, nil, err}在您的示例中,两种类型将是:type type1 struct {    Application struct {        Instance []struct {            InstanceID string `json:"instanceId"`        } `json:"instance"`    } `json:"application"`}type type2 struct {    Application struct {        Instance struct {            InstanceID string `json:"instanceId"`        } `json:"instance"`    } `json:"application"`}工作示例:https://play.golang.org/p/Kma32gWfghb

慕的地10843

一个更干净的解决方案是自定义的拆包器:type Instances []Instancefunc (i *Instances) UnmarshalJSON(in []byte) error {   if len(in)>0 && in[0]=='[' {     var a []Instance     if err:=json.Unmarshal(in,&a); err!=nil {        return err     }     *i=a     return nil  }  var s Instance  if err:=json.Unmarshal(in,&s) ; err!=nil {     return err  }  *i=[]Instance{s}  return nil}这会将对象解封为 1 的切片。@mkopriva提供了更紧凑的解决方案:func (i *Instances) UnmarshalJSON(in []byte) error {    if len(in) > 0 && in[0] == '[' {        return json.Unmarshal(in, (*[]Instance)(i))    }    *i = Instances{{}}    return json.Unmarshal(in, &(*i)[0])}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go