dongpu8935 2017-08-09 20:47
浏览 273

GoLang Unmarshal Extreme嵌套结构

Given the following structs

type UpdateConcert struct {
    FullConcert FullConcert
    Fields    map[string]interface{}
}

type FullConcert struct {
    DomConcert       *dom.Concert
    DomSingerID string `json:"domSingerID"`
    DomSingerGroupID string `json:"domSingerGroupID"`
}

With the dom.Concert being too large to post but contains int64, int, more nested struct slices, strings, string slices, and time pointers for a total of 90 fields on the dom.Concert (not including the lower nested structs). Also, all json tags are present on both the dom.Concert as well as all nested structs on it.

When doing:

    var arrayToUnmarshalInto []UpdateConcert
    var bytes []byte
    bytes, err = json.Marshal(concerts)
    if err != nil {
        return errors.Wrap(err, "Failed to marshal concerts")
    }

    byteLength := len(bytes)
    str := string(bytes[:byteLength])
    err = json.Unmarshal(bytes, &arrayToUnmarshalInto)
    if err != nil {
        return errors.Wrap(err, "Failed to unmarshal concerts")
    }
    fmt.Printf("%v", str)
    fmt.Printf("%v", arrayToUnmarshalInto)
    return nil

While using the standard json.Marshal from the "encoding/json" package we can get a valid byte slice back, and when casting to a string from the byte slice we see the expected stringified JSON. However, the arrayToUnmarshalInto produces an empty slice. And when inspecting an individual UpdateConcert struct we see only default values and nil. However, when iterating over a map[string]inteface{} that we have unmarshaled into, we can successfully print all values.

How would one go about writing a custom unmarshal for such a varied and deeply nested struct?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?