dongqian5384 2016-08-18 13:28
浏览 26
已采纳

解组json可选元素Golang

The data I got are xlsx rows in this format: row 1 value 1 is the first cell (A1), row 1 value 2 is the second cell (B1) etc. Any cell can (but doesn't need to) contain styles e.g. Border, Fill etc. .Styles will be in curly brackets like in this data: [[["row 1 value 1",{"Border":null,"Fill":{"arrFill":{"BgColor":"red"}},"Font":null,"ApplyBorder":null}],"row 1 value 2","row 1 value 3"],["row 2 value 1","row 2 value 2","row 2 value 3"]]

How to unmarshal this data to get also the not neccessary style map?

Playground

  • 写回答

1条回答 默认 最新

  • doulaobi7988 2016-08-18 14:58
    关注

    You may need to use the reflect package to check data types in the resulting array/map. It looks tedious, but seems that is the only way out. I have pasted below some quick and dirt, but runnable code to illustrate the same:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "reflect"
    )
    
    func parsePrintArray(y interface{}) {
        var dummy []interface{}
        var dummy2 map[string]interface{}
        if reflect.TypeOf(y) == reflect.TypeOf("") {
            fmt.Printf("%s ", y)
            return
        }
        if reflect.TypeOf(y) == reflect.TypeOf(dummy2) {
            x := y.(map[string]interface{})
            for ks, vi := range x {
                fmt.Printf("
    dumping for key %s ", ks)
                if vi != nil {
                    parsePrintArray(vi)
                }
            }
            return
        }
        for k, yl := range y.([]interface{}) {
            if reflect.TypeOf(yl) == reflect.TypeOf(dummy) {
                fmt.Println("")
                parsePrintArray(yl.([]interface{}))
            } else if reflect.TypeOf(yl) == reflect.TypeOf("") {
                fmt.Printf("%v: %s ", k, yl.(string))
            } else if reflect.TypeOf(yl) == reflect.TypeOf(dummy2) {
                x := yl.(map[string]interface{})
                for ks, vi := range x {
                    fmt.Printf("
    dumping for key %s ", ks)
                    if vi != nil {
                        parsePrintArray(vi)
                    }
                }
            }
        }
    }
    
    func main() {
    
        x := "[[[\"row 1 value1\",{\"Border\":null,\"Fill\":{\"arrFill\":{\"BgColor\":\"red\"}},\"Font\":null,\"ApplyBorder\":null}],\"row 1 value2\",\"row 1 value 3\"],[\"row 2 value 1\",\"row 2 value 2\",\"row 2 value 3\"]]"
    
        var y []interface{}
        err := json.Unmarshal([]byte(x), &y)
        if err != nil {
            panic(err)
        }
        parsePrintArray(y)
        fmt.Println("")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目