FAQ
I am doing a http test,send different type responses,server return common
results contains specific responses,struct like this
type Resp struct{
     Code string `json:"code"`
     Msg string `json:"msg"`
     Data interface{} `json:"data"`//Data are different type responses for
different requests
}


I want to merge a specific response from the Data

func DoCReq(cReq CRequest)*CResponse{
     reqStr,err := json.Marshal(cReq)
     data ,err := DoPost(C_URL,reqStr)
     resp := &Resp{}
     cresp := &CResp{}
     if err == nil{
       json.Unmarshal(data, resp)
       cresp = ..........//get the
     }
     return &cresp



}
func DoBReq(bReq BRequest)*BResponse{
     reqStr,err := json.Marshal(bReq)
     data ,err := DoPost(B_URL,reqStr)
     resp := &Resp{}
     bresp := &BResp{}
     if err == nil{
       json.Unmarshal(data, resp)
       bresp = ..........
     }
     return &bresp
}

and so on......
I tried to do like this:
func (cReq CRequest)CResponse{
     reqStr,err := json.Marshal(cReq)
     data ,err := DoPost(C_URL,reqStr)
     resp := &Resp{}
     cresp := &CResp{}
     if err == nil{
       json.Unmarshal(data, resp)
       middata,err := json.Marshal(resp.Data)// convert the interface to
[]byte,the []byte data is right
       json.Unmarshal(rsp,aResp)//but the response is {<nil> <nil> <nil>
<nil> []}
     }

}
I can do like this
type cResp struct{
     Code string `json:"code"`
     Msg string `json:"msg"`
     Data CResponse `json:"data"`
}
type bResp struct{
     Code string `json:"code"`
     Msg string `json:"msg"`
     Data BResponse `json:"data"`
}

it works,but this means I shoud define a struct for each response;
How should i do?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Approaching at Apr 26, 2016 at 8:46 am
    I am doing a http test,send different type responses,server return common
    results contains specific responses,struct like this
    type Resp struct{
         Code string `json:"code"`
         Msg string `json:"msg"`
         Data interface{} `json:"data"`//Data are different type responses for
    different requests
    }



    I want to merge a specific response from the Data

    func DoCReq(cReq CRequest)*CResponse{
         reqStr,err := json.Marshal(cReq)
         data ,err := DoPost(C_URL,reqStr)
         resp := &Resp{}
         cresp := &CResp{}
         if err == nil{
           json.Unmarshal(data, resp)
           cresp = ..........//get the resp
         }
         return &cresp
    }
    func DoBReq(bReq BRequest)*BResponse{
         reqStr,err := json.Marshal(bReq)
         data ,err := DoPost(B_URL,reqStr)
         resp := &Resp{}
         bresp := &BResp{}
         if err == nil{
           json.Unmarshal(data, resp)
           bresp = ..........
         }
         return &bresp
    }


    and so on......
    I tried to do like this:
    func (cReq CRequest)CResponse{
         reqStr,err := json.Marshal(cReq)
         data ,err := DoPost(C_URL,reqStr)
         resp := &Resp{}
         cresp := &CResp{}
         if err == nil{
           json.Unmarshal(data, resp)
           middata,err := json.Marshal(resp.Data)// convert the interface to
    []byte,the []byte data is right
           json.Unmarshal(middata,aResp)//but the response is {<nil> <nil> <nil>
    <nil> []}
         }

    }

    I can do like this
    type cResp struct{
         Code string `json:"code"`
         Msg string `json:"msg"`
         Data CResponse `json:"data"`
    }
    type bResp struct{
         Code string `json:"code"`
         Msg string `json:"msg"`
         Data BResponse `json:"data"`
    }



    it works,but this means I shoud define a struct for each response;
    How should i do?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Pierre Curto at Apr 26, 2016 at 8:58 am
    Hello,

    Set your interface{} with the correct type to decode into.
    I.e.
    func DoBReq(bReq BRequest)*BResponse{
         reqStr,err := json.Marshal(bReq)
         data ,err := DoPost(B_URL,reqStr)
         resp := &Resp{Data: BResp{}}
         if err == nil{
           err = json.Unmarshal(data, resp)
         }
         return &bresp
    }

    I have left out error checking which you should really do at every step.

    Le mardi 26 avril 2016 10:46:53 UTC+2, appro...@codinglife.me a écrit :
    I am doing a http test,send different type responses,server return common
    results contains specific responses,struct like this
    type Resp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data interface{} `json:"data"`//Data are different type responses for
    different requests
    }



    I want to merge a specific response from the Data

    func DoCReq(cReq CRequest)*CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    cresp = ..........//get the resp
    }
    return &cresp
    }
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{}
    bresp := &BResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    bresp = ..........
    }
    return &bresp
    }


    and so on......
    I tried to do like this:
    func (cReq CRequest)CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    middata,err := json.Marshal(resp.Data)// convert the interface to
    []byte,the []byte data is right
    json.Unmarshal(middata,aResp)//but the response is {<nil> <nil>
    <nil> <nil> []}
    }

    }

    I can do like this
    type cResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data CResponse `json:"data"`
    }
    type bResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data BResponse `json:"data"`
    }



    it works,but this means I shoud define a struct for each response;
    How should i do?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Approaching at Apr 26, 2016 at 10:12 am
    Hi,
    Thanks for your reply.Maybe I didn't make it clear.
           The "data" is the httpresponse, i can get a common Response as
    resp;and the resp contains a specific response as aresp\bresp,now i can
    get the resp,but i can't get the specific resp

    在 2016年4月26日星期二 UTC+8下午4:59:02,pierre...@gmail.com写道:
    Hello,

    Set your interface{} with the correct type to decode into.
    I.e.
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{Data: BResp{}}
    if err == nil{
    err = json.Unmarshal(data, resp)
    }
    return &bresp
    }

    I have left out error checking which you should really do at every step.

    Le mardi 26 avril 2016 10:46:53 UTC+2, appro...@codinglife.me a écrit :
    I am doing a http test,send different type responses,server return common
    results contains specific responses,struct like this
    type Resp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data interface{} `json:"data"`//Data are different type responses
    for different requests
    }



    I want to merge a specific response from the Data

    func DoCReq(cReq CRequest)*CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    cresp = ..........//get the resp
    }
    return &cresp
    }
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{}
    bresp := &BResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    bresp = ..........
    }
    return &bresp
    }


    and so on......
    I tried to do like this:
    func (cReq CRequest)CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    middata,err := json.Marshal(resp.Data)// convert the interface to
    []byte,the []byte data is right
    json.Unmarshal(middata,aResp)//but the response is {<nil> <nil>
    <nil> <nil> []}
    }

    }

    I can do like this
    type cResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data CResponse `json:"data"`
    }
    type bResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data BResponse `json:"data"`
    }



    it works,but this means I shoud define a struct for each response;
    How should i do?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • C Banning at Apr 26, 2016 at 10:35 am
    Might try something like this: https://play.golang.org/p/0n3pmsnygK

    On Tuesday, April 26, 2016 at 5:12:07 AM UTC-5, appro...@codinglife.me
    wrote:
    Hi,
    Thanks for your reply.Maybe I didn't make it clear.
    The "data" is the httpresponse, i can get a common Response as
    resp;and the resp contains a specific response as aresp\bresp,now i can
    get the resp,but i can't get the specific resp

    在 2016年4月26日星期二 UTC+8下午4:59:02,pierre...@gmail.com写道:
    Hello,

    Set your interface{} with the correct type to decode into.
    I.e.
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{Data: BResp{}}
    if err == nil{
    err = json.Unmarshal(data, resp)
    }
    return &bresp
    }

    I have left out error checking which you should really do at every step.

    Le mardi 26 avril 2016 10:46:53 UTC+2, appro...@codinglife.me a écrit :
    I am doing a http test,send different type responses,server return
    common results contains specific responses,struct like this
    type Resp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data interface{} `json:"data"`//Data are different type responses
    for different requests
    }



    I want to merge a specific response from the Data

    func DoCReq(cReq CRequest)*CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    cresp = ..........//get the resp
    }
    return &cresp
    }
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{}
    bresp := &BResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    bresp = ..........
    }
    return &bresp
    }


    and so on......
    I tried to do like this:
    func (cReq CRequest)CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    middata,err := json.Marshal(resp.Data)// convert the interface to
    []byte,the []byte data is right
    json.Unmarshal(middata,aResp)//but the response is {<nil> <nil>
    <nil> <nil> []}
    }

    }

    I can do like this
    type cResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data CResponse `json:"data"`
    }
    type bResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data BResponse `json:"data"`
    }



    it works,but this means I shoud define a struct for each response;
    How should i do?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Approaching at Apr 26, 2016 at 11:17 am
    Hi,Banning;
          When i make assertion of the type of Data,it doesn't treat it as the
    specific Rsponse,such as the ARSP\BRSP.I used nick's method,and the problem
    resol
      在 2016年4月26日星期二 UTC+8下午6:35:05,C Banning写道:
    Might try something like this: https://play.golang.org/p/0n3pmsnygK

    On Tuesday, April 26, 2016 at 5:12:07 AM UTC-5, appro...@codinglife.me
    wrote:
    Hi,
    Thanks for your reply.Maybe I didn't make it clear.
    The "data" is the httpresponse, i can get a common Response as
    resp;and the resp contains a specific response as aresp\bresp,now i can
    get the resp,but i can't get the specific resp

    在 2016年4月26日星期二 UTC+8下午4:59:02,pierre...@gmail.com写道:
    Hello,

    Set your interface{} with the correct type to decode into.
    I.e.
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{Data: BResp{}}
    if err == nil{
    err = json.Unmarshal(data, resp)
    }
    return &bresp
    }

    I have left out error checking which you should really do at every step.

    Le mardi 26 avril 2016 10:46:53 UTC+2, appro...@codinglife.me a écrit :
    I am doing a http test,send different type responses,server return
    common results contains specific responses,struct like this
    type Resp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data interface{} `json:"data"`//Data are different type responses
    for different requests
    }



    I want to merge a specific response from the Data

    func DoCReq(cReq CRequest)*CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    cresp = ..........//get the resp
    }
    return &cresp
    }
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{}
    bresp := &BResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    bresp = ..........
    }
    return &bresp
    }


    and so on......
    I tried to do like this:
    func (cReq CRequest)CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    middata,err := json.Marshal(resp.Data)// convert the interface
    to []byte,the []byte data is right
    json.Unmarshal(middata,aResp)//but the response is {<nil> <nil>
    <nil> <nil> []}
    }

    }

    I can do like this
    type cResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data CResponse `json:"data"`
    }
    type bResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data BResponse `json:"data"`
    }



    it works,but this means I shoud define a struct for each response;
    How should i do?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nick Craig-Wood at Apr 26, 2016 at 9:33 am
    You want to delay the decoding using json.RawMessage

    See: https://golang.org/pkg/encoding/json/#RawMessage and the very
    useful example.
    On 26/04/16 09:46, approaching@codinglife.me wrote:
    I am doing a http test,send different type responses,server return
    common results contains specific responses,struct like this
    type Respstruct{
    Codestring`json:"code"`
    Msg string`json:"msg"`
    Datainterface{}`json:"data"`//Data are different type responses
    for different requests
    }

    I want to merge a specific response from the Data
    func DoCReq(cReq CRequest)*CResponse{
    reqStr,err :=json.Marshal(cReq)
    data ,err :=DoPost(C_URL,reqStr)
    resp :=&Resp{}
    cresp :=&CResp{}
    iferr ==nil{
    json.Unmarshal(data,resp)
    cresp =..........//get the resp
    }
    return&cresp
    }
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err :=json.Marshal(bReq)
    data ,err :=DoPost(B_URL,reqStr)
    resp :=&Resp{}
    bresp :=&BResp{}
    iferr ==nil{
    json.Unmarshal(data,resp)
    bresp =..........
    }
    return&bresp
    }


    andso on......
    I tried to dolike this:
    func (cReq CRequest)CResponse{
    reqStr,err :=json.Marshal(cReq)
    data ,err :=DoPost(C_URL,reqStr)
    resp :=&Resp{}
    cresp :=&CResp{}
    iferr ==nil{
    json.Unmarshal(data,resp)
    middata,err :=json.Marshal(resp.Data)// convert the interface to
    []byte,the []byte data is right
    json.Unmarshal(middata,aResp)//but the response is {<nil> <nil>
    <nil> <nil> []}
    }

    }

    I can do like this
    type cResp struct{
    Codestring`json:"code"`
    Msg string`json:"msg"`
    DataCResponse`json:"data"`
    }
    type bResp struct{
    Codestring`json:"code"`
    Msg string`json:"msg"`
    DataBResponse`json:"data"`
    }

    it works,but this means I shoud define a struct for each response;
    How should i do?
    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts+unsubscribe@googlegroups.com
    For more options, visit https://groups.google.com/d/optout.

    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Approaching at Apr 26, 2016 at 11:02 am
    Hi,Nick
      I used json.RawMessage,and it works,thanks very much!But I don't
    understand why this code work,this code is a simplified version
    <https://www.baidu.com/link?url=oDcktvdu-acsmN9qQw6PE0SUCnz_MkvYwtOhr1a9JDCbt51TM_5j08RMqSm83kP2vpW5hN7eEkYHaqPidrLxztlaTD1AWmhxnlhZVQYCZZpn80ps_a5qeEAHj42UOM6B&wd=&eqid=9a470ae50001721e00000004571f49ce>
    ;

    import (
         "fmt"
         "encoding/json"
    )
    type commonRsp struct{
         A string `json:"a"`
         B specialRsp `json:"b"`
    }
    type middleRsp struct{
         A string `json:"a"`
         B interface{} `json:"b"`//also a interface{}
    }
    type specialRsp struct{
         A string `json:"a"`
         B string `json:"b"`
    }
    func Test(){


         speRsp := &specialRsp{
             A : "Test",
             B : "Test",
         }
         comRsp := &commonRsp{
             A : "Test",
             B : *speRsp,
         }
         midRsp := &middleRsp{}
         data,err := json.Marshal(comRsp)
         if err == nil{
             json.Unmarshal(data,midRsp)
             data,_ := json.Marshal(midRsp.B)
             speRsp2 := &specialRsp{
             }
             json.Unmarshal(data,speRsp2)
             fmt.Println(speRsp2.A)




         }else{
             fmt.Println("Fail")


         }
    }


    在 2016年4月26日星期二 UTC+8下午5:34:10,Nick Craig-Wood写道:
    You want to delay the decoding using json.RawMessage

    See: https://golang.org/pkg/encoding/json/#RawMessage and the very useful
    example.

    On 26/04/16 09:46, appro...@codinglife.me <javascript:> wrote:

    I am doing a http test,send different type responses,server return common
    results contains specific responses,struct like this
    type Resp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data interface{} `json:"data"`//Data are different type responses for
    different requests
    }



    I want to merge a specific response from the Data

    func DoCReq(cReq CRequest)*CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    cresp = ..........//get the resp
    }
    return &cresp
    }
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{}
    bresp := &BResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    bresp = ..........
    }
    return &bresp
    }


    and so on......
    I tried to do like this:
    func (cReq CRequest)CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    middata,err := json.Marshal(resp.Data)// convert the interface to
    []byte,the []byte data is right
    json.Unmarshal(middata,aResp)//but the response is {<nil> <nil>
    <nil> <nil> []}
    }

    }

    I can do like this
    type cResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data CResponse `json:"data"`
    }
    type bResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data BResponse `json:"data"`
    }



    it works,but this means I shoud define a struct for each response;
    How should i do?
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.



    --
    Nick Craig-Wood <n...@craig-wood.com> <javascript:> -- http://www.craig-wood.com/nick
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Nick Craig-Wood at Apr 26, 2016 at 1:49 pm

    On 26/04/16 12:02, approaching@codinglife.me wrote:
    I used json.RawMessage,and it works,thanks very much! Great
    But I don't
    understand why this code work,this code is a simplified version
    It works because you unmarshal to a `interface {}` then marshal back to
    JSON, and then unmarshal that back into `specialRsp{}`.

    That is rather inefficient but it does work!
    import(
    "fmt"
    "encoding/json"
    )
    type commonRsp struct{
    A string`json:"a"`
    B specialRsp `json:"b"`
    }
    type middleRsp struct{
    A string`json:"a"`
    B interface{}`json:"b"`//also a interface{}
    }
    type specialRsp struct{
    A string`json:"a"`
    B string`json:"b"`
    }
    func Test(){


    speRsp :=&specialRsp{
    A :"Test",
    B :"Test",
    }
    comRsp :=&commonRsp{
    A :"Test",
    B :*speRsp,
    }
    midRsp :=&middleRsp{}
    data,err :=json.Marshal(comRsp)
    data = {"a":"Test","b":{"a":"Test","b":"Test"}}
    iferr ==nil{
    json.Unmarshal(data,midRsp)
    midRsp.B is an interface{} so this will be unmarshalled into
    map[string]string

    midRsp = &{A:Test B:map[a:Test b:Test]}
    data,_ :=json.Marshal(midRsp.B)
    data here is now JSON

    data = {"a":"Test","b":"Test"}
    speRsp2 :=&specialRsp{
    }
    And now we decode the JSON data back into specialRsp
    json.Unmarshal(data,speRsp2)
    speRsp = &{A:Test B:Test}
    fmt.Println(speRsp2.A)

    }else{
    fmt.Println("Fail")
    }
    }
    See: http://play.golang.org/p/OAUVlnmKsW

    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • C Banning at Apr 26, 2016 at 2:08 pm
    Or perhaps something like: http://play.golang.org/p/9N1hj6BwHn - save a
    json.Marshal.
    On Tuesday, April 26, 2016 at 8:49:24 AM UTC-5, Nick Craig-Wood wrote:
    On 26/04/16 12:02, appro...@codinglife.me <javascript:> wrote:
    I used json.RawMessage,and it works,thanks very much! Great
    But I don't
    understand why this code work,this code is a simplified version
    It works because you unmarshal to a `interface {}` then marshal back to
    JSON, and then unmarshal that back into `specialRsp{}`.

    That is rather inefficient but it does work!
    import(
    "fmt"
    "encoding/json"
    )
    type commonRsp struct{
    A string`json:"a"`
    B specialRsp `json:"b"`
    }
    type middleRsp struct{
    A string`json:"a"`
    B interface{}`json:"b"`//also a interface{}
    }
    type specialRsp struct{
    A string`json:"a"`
    B string`json:"b"`
    }
    func Test(){


    speRsp :=&specialRsp{
    A :"Test",
    B :"Test",
    }
    comRsp :=&commonRsp{
    A :"Test",
    B :*speRsp,
    }
    midRsp :=&middleRsp{}
    data,err :=json.Marshal(comRsp)
    data = {"a":"Test","b":{"a":"Test","b":"Test"}}
    iferr ==nil{
    json.Unmarshal(data,midRsp)
    midRsp.B is an interface{} so this will be unmarshalled into
    map[string]string

    midRsp = &{A:Test B:map[a:Test b:Test]}
    data,_ :=json.Marshal(midRsp.B)
    data here is now JSON

    data = {"a":"Test","b":"Test"}
    speRsp2 :=&specialRsp{
    }
    And now we decode the JSON data back into specialRsp
    json.Unmarshal(data,speRsp2)
    speRsp = &{A:Test B:Test}
    fmt.Println(speRsp2.A)

    }else{
    fmt.Println("Fail")
    }
    }
    See: http://play.golang.org/p/OAUVlnmKsW

    --
    Nick Craig-Wood <ni...@craig-wood.com <javascript:>> --
    http://www.craig-wood.com/nick
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • FeiLiang at Apr 26, 2016 at 12:34 pm
    sorry, i can not understand....

    在 2016年4月26日星期二 UTC+8下午4:00:28,appro...@codinglife.me写道:
    I am doing a http test,send different type responses,server return common
    results contains specific responses,struct like this
    type Resp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data interface{} `json:"data"`//Data are different type responses for
    different requests
    }


    I want to merge a specific response from the Data

    func DoCReq(cReq CRequest)*CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    cresp = ..........//get the
    }
    return &cresp



    }
    func DoBReq(bReq BRequest)*BResponse{
    reqStr,err := json.Marshal(bReq)
    data ,err := DoPost(B_URL,reqStr)
    resp := &Resp{}
    bresp := &BResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    bresp = ..........
    }
    return &bresp
    }

    and so on......
    I tried to do like this:
    func (cReq CRequest)CResponse{
    reqStr,err := json.Marshal(cReq)
    data ,err := DoPost(C_URL,reqStr)
    resp := &Resp{}
    cresp := &CResp{}
    if err == nil{
    json.Unmarshal(data, resp)
    middata,err := json.Marshal(resp.Data)// convert the interface to
    []byte,the []byte data is right
    json.Unmarshal(rsp,aResp)//but the response is {<nil> <nil> <nil>
    <nil> []}
    }

    }
    I can do like this
    type cResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data CResponse `json:"data"`
    }
    type bResp struct{
    Code string `json:"code"`
    Msg string `json:"msg"`
    Data BResponse `json:"data"`
    }

    it works,but this means I shoud define a struct for each response;
    How should i do?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 26, '16 at 8:00a
activeApr 26, '16 at 2:08p
posts11
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase