猿问

Golang:Unmarshal函数,可以处理2种不同的结构

寻求帮助。


假设我有以下结构。


type phoneStruct struct {

    PhoneID    string      `json:"id"`

    Carrier      string    `json:"carrier"`

}


type carStruct struct {

    CarID       string    `json:"id"`

    Model       string    `json:"model"`

}

两种截然不同的结构。但可以预测。


现在在我的代码中,我正在做的是执行POST请求,然后解析我返回的内容的主体,以便我可以对其进行一些测试断言。但为了简单起见,假设我有这个JSON


    var jsonPhone = `{

            "id": "123",

            "carrier": "Rogers"

        }`


    var jsonCar = `{

            "id": "foobar-fewf-434-fewf",

            "model": "Civic"

        }`


我想将这个json与我的结构相关联,所以我有以下调用:


p, err := parsePhone(jsonPhone)

c, err := parseCar(jsonCar)

这些函数看起来像这样


func parsePhone(body []byte) (*phoneStruct, error) {

    var p = new(phoneStruct)

    err := json.Unmarshal(body, p)

    return p, err

}


func parseCar(body []byte) (*carStruct, error) {

    var c = new(carStruct)

    err := json.Unmarshal(body, c)

    return c, err

}

..我想知道是否有一种方法可以只拥有一个结构。我知道两个或多个结构之间是否存在一些共性。或者如何解析未知结构。但是如果我知道结构,就无法找到资源。我怎么能有一个看起来像这样的函数..


func parseAnything(body []byte (<pointer to the struct>, error)

    var c = new(someStruct)

    err := json.Unmarshal(body, c)

    return c, err

}

请注意,我将始终知道我在调用什么,因此我可以在我的解析函数中执行条件开关语句,该语句显示“if car..如果电话..'但我不确定如何定义回报。


慕标琳琳
浏览 170回答 2
2回答

子衿沉夜

编写函数以获取指向结果的指针:func parseAnything(body []byte, c interface{}) error {&nbsp; &nbsp; return json.Unmarshal(body, c)}像这样使用它:var p phoneStructif err := parseAnything(jsonPhone, &p); err != nil {&nbsp; &nbsp; // handle error}// p has unmarshaled phonevar c carStructif err := parseAnything(jsonCar, &c); err != nil {&nbsp; &nbsp;// handle error}// c has unmarshaled car

慕村225694

我不知道你到底想建立什么,但会尝试根据我的理解提供一些见解。如果您尝试对两个结构使用相同的解析器,则它们可能有一些共同点。可能它们被一起用于您应用程序的某些用例。因此,如果它们一起使用,它们应该实现一些接口,这些接口表示这些结构具有共同的特征集(可能与数据解析有关,也可能不相关)。而且,正如你所说,你事先知道你需要什么类型的结构,所以这样做应该很容易://The interface that represents what the structs have in common.&nbsp;//I named it "Parser" because it's the info I have. It probably should have another (better) nametype Parser interface {&nbsp; Parse([]byte) (Parser, error)}type Phone struct {&nbsp; &nbsp; PhoneID&nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; `json:"id"`&nbsp; &nbsp; Carrier&nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; `json:"carrier"`}type Car struct {&nbsp; &nbsp; CarID&nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; `json:"id"`&nbsp; &nbsp; Model&nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; `json:"model"`}func (p *Phone) Parse (body []byte) (Parser, error) {&nbsp; &nbsp; err := json.Unmarshal(body, p)&nbsp; &nbsp; return p, err}func (c *Car) Parse (body []byte) (Parser, error) {&nbsp; &nbsp; err := json.Unmarshal(body, c)&nbsp; &nbsp; return c, err}func main() {&nbsp; &nbsp; c := Car{}&nbsp; &nbsp; var jsonCar = `{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "123",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "model": "Civic"&nbsp; &nbsp; &nbsp; &nbsp; }`&nbsp; &nbsp; res, _ := c.Parse([]byte(jsonCar))&nbsp; &nbsp; fmt.Println(res)&nbsp; &nbsp; p := Phone{}&nbsp; &nbsp; var jsonPhone = `{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "123",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "carrier": "Rogers"&nbsp; &nbsp; &nbsp; &nbsp; }`&nbsp; &nbsp; res, _ = p.Parse([]byte(jsonPhone))&nbsp; &nbsp; fmt.Println(res)&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;}
随时随地看视频慕课网APP

相关分类

Go
我要回答