downloadbooks_2014 2018-04-04 00:37
浏览 49
已采纳

来自JSON的不同数据结构

My question is, I have a JSON that returns me a list of objects. But sometimes this list returns me a single object in case the det.

I try to use the pattern below

Det []struct {
                    NItem string `json:"-nItem"`
                    Prod  struct {
                        CProd    string `json:"cProd"`
                        CEST     string `json:"CEST"`
                        Cfop     string `json:"CFOP"`
                        UCom     string `json:"uCom"`
                        QCom     string `json:"qCom"`
                        IndTot   string `json:"indTot"`
                        VProd    string `json:"vProd"`
                        CEANTrib string `json:"cEANTrib"`
                        UTrib    string `json:"uTrib"`
                        NCM      string `json:"NCM"`
                        CEAN     string `json:"cEAN"`
                        XProd    string `json:"xProd"`
                        VUnCom   string `json:"vUnCom"`
                        QTrib    string `json:"qTrib"`
                        VUnTrib  string `json:"vUnTrib"`
                    } `json:"prod"`
                    Imposto struct {
                        VTotTrib string `json:"vTotTrib"`
                        ICMS     struct {
                            ICMS00 struct {
                                Orig  string `json:"orig"`
                                CST   string `json:"CST"`
                                ModBC string `json:"modBC"`
                                VBC   string `json:"vBC"`
                                PICMS string `json:"pICMS"`
                                VICMS string `json:"     vICMS"`
                            } `json:"ICMS00"`
                        } `json:"ICMS"`
                        IPI struct {
                            CEnq    string `json:"cEnq"`
                            IPITrib struct {
                                CST  string `json:"CST"`
                                VBC  string `json:"vBC"`
                                PIPI string `json:"pIPI"`
                                VIPI string `json:"vIPI"`
                            } `json:"IPITrib"`
                        } `json:"IPI"`
                        PIS struct {
                            PISAliq struct {
                                CST  string `json:"CST"`
                                VBC  string `json:"vBC"`
                                PPIS string `json:"pPIS"`
                                VPIS string `json:"vPIS"`
                            } `json:"PISAliq"`
                        } `json:"PIS"`
                        COFINS struct {
                            COFINSAliq struct {
                                CST     string `json:"CST     "`
                                VBC     string `json:"vBC"`
                                PCOFINS string `json:"pCOFINS"`
                                VCOFINS string `json:"vCOFINS"`
                            } `json:"COFINSAliq"`
                        } `json:"COFINS"`
                    } `json:"imposto"`
                } `json:"det"`

but works perfectly, but when the det object receives only element the unmarshal does not work, only works if I remove the slice of struct like that

Det struct {
                    NItem string `json:"-nItem"`
                    Prod  struct {
                        CProd    string `json:"cProd"`
                        CEST     string `json:"CEST"`
                        Cfop     string `json:"CFOP"`
                        UCom     string `json:"uCom"`
                        QCom     string `json:"qCom"`
                        IndTot   string `json:"indTot"`
                        VProd    string `json:"vProd"`
                        CEANTrib string `json:"cEANTrib"`
                        UTrib    string `json:"uTrib"`
                        NCM      string `json:"NCM"`
                        CEAN     string `json:"cE

The whole structure looks like this.

SomeStruct struct { ID string json:"-Id" Ide struct {...} json:"ide" Total struct { ICMSTot struct {...} json:"ICMSTot" } json:"total" Cobr struct { Dup struct {....} json:"dup" } json:"cobr" Versao string json:"-versao" Emit struct { ... } json:"enderEmit" Det []struct { NItem string json:"-nItem" Prod struct { CProd string json:"cProd" CEST string json:"CEST" Cfop string json:"CFOP" UCom string json:"uCom" QCom string json:"qCom" IndTot string json:"indTot" VProd string json:"vProd" CEANTrib string json:"cEANTrib" UTrib string json:"uTrib" NCM string json:"NCM" CEAN string json:" cEAN" XProd string json:"xProd" VUnCom string json:"vUnCom" QTrib string json:"qTrib" VUnTrib string json:"vUnTrib" } json:"prod"

  • 写回答

1条回答 默认 最新

  • dongmi3203 2018-04-04 03:39
    关注

    Decode the JSON as a slice if the first non-whitespace byte in the input is [. Otherwise, decode the JSON as a struct.

    func decode(data []byte) ([]Det, error) {
        data = bytes.TrimSpace(data)
        if len(data) == 0 {
            return nil, errors.New("empty")
        }
        if data[0] == '[' {
            var v []Det
            err := json.Unmarshal(data, &v)
            return v, err
        }
        var v [1]Det
        err := json.Unmarshal(data, &v[0])
        return v[:], err
    }
    

    Playground example

    If the Det value is a member of a struct, then use RawMessage to capture the JSON and then use the function above.

     type SomeStruct struct {
        ....
        DetRaw json.RawMessage `json:"Det"`
        Det []Det `json:"-"`
        ....
    }
    
    var v SomeStruct
    if err := json.Unmarshal(data, &v); err != nil {
       // handle error
    }
    v.Det, err = decode([]byte(v.DetRaw))
    if err != nil {
       // handle error
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp