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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码