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

解组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

图片转代码服务由CSDN问答提供 功能建议

我得到的数据是这种格式的xlsx行:row 1 value 1是第一个单元格(A1),row 1值2是第二个单元格(B1)等。任何单元格都可以(但不需要)包含样式,例如 边框,填充等。样式将用大括号括起来,例如此数据: [[[“行1值1”,{“边框”:null,“填充”:{“ arrFill”:{“ BgColor” :“ red”}},“ Font”:null,“ ApplyBorder”:null}],“行1值2”,“行1值3”],[“行2值1”,“行2值2” ,“行2值3”]]]

如何解组此数据以获取不必要的样式图?

< 一个href =“ https://play.golang.org/p/s451HUOETm” rel =“ nofollow”>游乐场

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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("")
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题