将JSON目录树转换为带缩进的纯文本在GoLang中的实现

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

Converting a JSON dir tree into indented plain text in GoLang

问题

我有一个以JSON结构表示的目录树,我想要将其格式化为纯文本。在XML或YAML中进行格式化相对容易,但在纯文本中进行格式化比我想象的要困难得多。

JSON结构的格式如下:

  1. type File struct {
  2. Name string `json:"Name"`
  3. Children []*File `json:"Children"`
  4. }

由于JSON结构允许有"children",所以JSON是嵌套的,而且由于它是一个目录树,我不知道嵌套会有多深(在合理范围内)。

我需要转换后的JSON看起来像这样:

  1. base_dir
  2. sub_dir_1
  3. sub_dir_2
  4. file_in_sub_dir_2
  5. sub_dir_3
  6. ...

有人能告诉我如何以相对简单的方式实现这个目标吗?目前我只能使用大量循环和制表符进行强制缩进,但我确信在Go语言中有更优雅的方式。

英文:

I have a dir tree in a JSON struct that I'm trying to format in plain text. Formatting it in XML or YAML is pretty easy. Formatting it in plain text is much harder than I thought.

The JSON struct is formatted like this :

  1. type File struct {
  2. Name string `json:"Name"`
  3. Children []*File `json:"Children"`
  4. }

Since the JSON structure allows for 'children', the JSON is nested, and since it's a dir tree, I don't know how deep the nesting will get (within reason).

I need the converted JSON to look like this :

  1. base_dir
  2. sub_dir_1
  3. sub_dir_2
  4. file_in_sub_dir_2
  5. sub_dir_3
  6. ...

Can anyone tell me how this could be done in a reasonably simple way? Right now I'm having to brute force with lots of looping and indenting with tabs, and I'm just sure there's a more elegant way in Go.

答案1

得分: 3

编写一个函数来递归遍历目录树,打印文件及其子文件。在递归遍历树时增加缩进级别。

  1. func printFile(f *File, indent string) {
  2. fmt.Printf("%s%s\n", indent, f.Name)
  3. for _, f := range f.Children {
  4. printFile(f, indent+" ")
  5. }
  6. }

使用树的根节点调用该函数:

  1. printFile(root, "")

在 playground 上运行这段代码

英文:

Write a function to recurse down the directory tree printing a file and its children. Increase the indent level when recursing down the tree.

  1. func printFile(f *File, indent string) {
  2. fmt.Printf("%s%s\n", indent, f.Name)
  3. for _, f := range f.Children {
  4. printFile(f, indent+" ")
  5. }
  6. }

Call the function with the root of the tree:

  1. printFile(root, "")

<kbd>run the code on the playground</kbd>

答案2

得分: 2

你可以使用MarshalIndent来实现这个功能。这里有一个<kbd>go playground</kbd>的示例。

  1. instance := MyStruct{12344, "ohoasdoh", []int{1, 2, 3, 4, 5}}
  2. res, _ := json.MarshalIndent(instance, "", " ")
  3. fmt.Println(string(res))

这将会得到如下结果:

  1. {
  2. "Num": 12344,
  3. "Str": "ohoasdoh",
  4. "Arr": [
  5. 1,
  6. 2,
  7. 3,
  8. 4,
  9. 5
  10. ]
  11. }
英文:

You can achieve this with MarshalIndent. Here is a <kbd>go playground</kbd> example.

  1. instance := MyStruct{12344, &quot;ohoasdoh&quot;, []int{1, 2, 3, 4, 5}}
  2. res, _ := json.MarshalIndent(instance, &quot;&quot;, &quot; &quot;)
  3. fmt.Println(string(res))

Which will give you something like:

  1. {
  2. &quot;Num&quot;: 12344,
  3. &quot;Str&quot;: &quot;ohoasdoh&quot;,
  4. &quot;Arr&quot;: [
  5. 1,
  6. 2,
  7. 3,
  8. 4,
  9. 5
  10. ]
  11. }

huangapple
  • 本文由 发表于 2015年12月21日 11:58:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/34388758.html
匿名

发表评论

匿名网友

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

确定