dongyanjing5975 2018-04-01 07:47
浏览 98
已采纳

嵌套JSON中的相同结构

I got a problem with parsing JSON in golang where I receive response from API in JSON format which nests same form of JSON in multiple levels.

Response from API is following

{
    "podKategoria": {
        "podKategoriaTyp": "area",
        "nazwaWyswietlana": "Area",
        "podKategorie": [
            {
                "podKategoriaTyp": "somethingelse",
                "nazwaWyswietlana": "Display something else",
                "podKategoria": {
                    "podKategoriaTyp": "and other thing",
                    "nazwaWyswietlana": "Display and other thing",
                    "podKategorie": [
                        {
                            "podKategoriaTyp": "sub nd other thing",
                            "nazwaWyswietlana": "display nd other thing"
                        },
                        {
                            "podKategoriaTyp": "sub 2 nd other thing",
                            "nazwaWyswietlana": "display sub 2 nd other thing"
                        },
                        {
                            "podKategoriaTyp": "sub 3 nd other thing",
                            "nazwaWyswietlana": "display sub 3 nd other thing"
                        }
                    ]
                }
            },
            {
                "podKategoriaTyp": "another one",
                "nazwaWyswietlana": "display another one",
                "podKategoria": {
                    "podKategoriaTyp": "and and another one ",
                    "nazwaWyswietlana": "display it another one",
                    "podKategorie": [
                        {
                            "podKategoriaTyp": "sub 1 another one",
                            "nazwaWyswietlana": "display sub 1"
                        },
                        {
                            "podKategoriaTyp": "sub 2 another one",
                            "nazwaWyswietlana": "display sub 2"
                        },
                    ]
                }
            },
        ]
    }
}

Following this I have used https://mholt.github.io/json-to-go/ to try and create structure for me. The most appropriate approach seemed to be creating base structure like

type Struktury struct {
    PodKategorie PodKategoria `json:"podKategoria"`
}


type PodKategoria struct {
    PodKategoriaTyp string `json:"podKategoriaTyp"`
    NazwaWyswietlana     string `json:"nazwaWyswietlana"`
    PodKategorie   []struct {
        SubCategoryType string `json:"podKategoriaTyp"`
        DisplayName     string `json:"nazwaWyswietlana"`
    } `json:"podKategorie, omitempty"`
}

But as you can see JSON structure is a bit tricky and I'm kinda stuck trying to figure the best way to proper unmarshliing this JSON string :/

Initial attempt to just use defaults allows me to get root of items ( tested in playground https://play.golang.org/p/uQMnMGEtXD- ) therefore this brings me to idea that maybe custom unmarshall interface implemention is the way to go here ? Any pointers how to tackle this would be appreciated

  • 写回答

1条回答 默认 最新

  • duangai1941 2018-04-03 17:03
    关注

    Here is a solution that works, check playground

    type Categories []*Category
    type Category struct {
        Type string `json:"podKategoriaTyp"`
        Display string `json:"nazwaWyswietlana"`
        Categories Categories `json:"podKategorie"`
        SubCategory *Category `json:"podKategoria"`
    }
    
    func main() {
    
        cat := &Category{}
        err := json.Unmarshal([]byte(JSON), &cat)
        if err != nil {
            log.Fatal(err)
        }
        buf, err := json.Marshal(cat)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%s
    ", buf)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里