登录
首页 >  Golang >  Go问答

实现Golang多维切片转换为符合预期的JSON字符串输出

来源:stackoverflow

时间:2024-02-10 15:21:20 166浏览 收藏

各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题《实现Golang多维切片转换为符合预期的JSON字符串输出》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!

问题内容

在我的 go 代码中,我有多维字符串切片

input := [][]string{
    {"foods", "food by cooking technique", "baked food"},
    {"foods", "food by cooking technique", "baked beans"},
    {"foods", "food by type", "dried food"},
    {"drinks", "hot drinks", "tea"},
    {"drinks", "hot drinks", "herbal tea"},
    {"drinks", "cold drinks", "ice cream drinks"},
}

如何转换为 json 以便打印结果符合预期。也许用递归

{
    "data": [
      {
        "name": "foods",
        "data": [
          {
            "name": "food by cooking technique",
            "data": [
              { "name": "baked food" },
              { "name": "baked beans" }
            ]
          },
          {
            "name": "food by type",
            "data": [
              { "name": "dried food" }
            ]
          }
        ]
      },
      {
        "name": "drinks",
        "data": [
          {
            "name": "hot drinks",
            "data": [
              { "name": "tea" },
              { "name": "herbal tea" }
            ]
          },
          {
            "name": "cold drinks",
            "data": [
              { "name": "ice cream drinks" }
            ]
          }
        ]
      }
    ]
}

还没有想出这方面的想法,谢谢你的帮助


正确答案


速度不快,但可以理解的方法:

package main

import (
    "encoding/json"
    "log"
)

type mapStruct struct {
    Data map[string]*mapStruct
}

type dataStruct struct {
    Name string        `json:"name,omitempty"`
    Data []*dataStruct `json:"data,omitempty"`
}

func main() {

    input := [][]string{
        {"foods", "food by cooking technique", "baked food"},
        {"foods", "food by cooking technique", "baked beans"},
        {"foods", "food by type", "dried food"},
        {"drinks", "hot drinks", "tea"},
        {"drinks", "hot drinks", "herbal tea"},
        {"drinks", "cold drinks", "ice cream drinks"},
    }

    data := &mapStruct{}

    for _, v := range input {
        temp := data
        for _, vv := range v {
            if temp.Data == nil {
                temp.Data = map[string]*mapStruct{}
            }
            if _, ok := temp.Data[vv]; !ok {
                temp.Data[vv] = &mapStruct{}
            }
            temp = temp.Data[vv]
        }
    }

    output := &dataStruct{}

    fun(output, data)

    bts, err := json.MarshalIndent(output, "", "\t")
    if err != nil {
        log.Println(err)
        return
    }
    log.Println(string(bts))

}

func fun(d *dataStruct, m *mapStruct) {
    for k, v := range m.Data {
        d.Data = append(d.Data, &dataStruct{})
        d.Data[len(d.Data)-1].Name = k
        fun(d.Data[len(d.Data)-1], v)
    }
}

今天关于《实现Golang多维切片转换为符合预期的JSON字符串输出》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>