将Golang的多维切片转换为期望的输出字符串JSON。

huangapple go评论82阅读模式
英文:

Golang multidimensional slices into expected output string json

问题

在我的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" }
					]
				}
			]
		}
	]
}

我还没有想到解决这个问题的方法,感谢你的帮助。

英文:

In my Go code, I have multidimensional slices of string

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"},
}

how to convert to json so that the resulting print output is as expected. maybe with recursive

{
	"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" }
			]
		  }
		]
	  }
	]
}

still haven't come up with an idea regarding this, thanks for your help

答案1

得分: 1

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

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)
	}
}

希望对你有帮助!

英文:

Not fast, but an understandable approach:

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)
}
}

huangapple
  • 本文由 发表于 2022年6月29日 11:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/72795550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定