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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?