如何解析 TOML 中的嵌套数组/子表?
How to parse nested arrays / sub tables in TOML?
我从 Go 开始,官方 documentation 似乎更适合那些已经了解 Go 并且只想查找资料的人。我希望在这里朝着正确的方向稍微推动一下。
我想要做什么: 使用 BurntSushi 的 TOML 解析器解析 TOML
配置文件具有相同基本特征的元素。
卡住的地方: 我希望每个项目的各自部分与项目一起列出。到目前为止,我只能通过其索引获得其中之一。我正在寻找的是如何以列出相应索引的所有子部分而不是仅列出特定索引的方式进行设置。我可以用 [:]
得到一个 JSON 列表,但似乎无法调整它以获得 正常 输出。
最初我考虑过 [[item.part.001]]
等等,因为它在在线 JSON 解析器中看起来是正确的,但无法弄清楚如何正确地将其读入 Go。由于无论如何我都被卡住了,我对这两种类型都持开放态度,无论哪种方式效果最好。
提前致谢。以下是作为简化的最小工作示例的文件。
demo.toml
# — — — — — — — — — — — — — — — — — — — — — — —
# First Item
# — — — — — — — — — — — — — — — — — — — — — — —
[[item]]
itemname = "Fragments"
itemdesc = "This one can get a bit longer."
[item.attributes]
material = "Basematname"
light = "Lightname"
transpc = "full"
displace = "height"
[[item.part]]
partname = "Shard"
partlink = "active"
[[item.part]]
partname = "Tear"
partlink = "deferred"
[[item.part]]
partname = "crater"
partlink = "disabled"
# — — — — — — — — — — — — — — — — — — — — — — —
# Second Item
# — — — — — — — — — — — — — — — — — — — — — — —
[[item]]
itemname = "Splash"
itemdesc = "This one also can get a bit longer."
[item.attributes]
material = "Other Basematname"
light = "Other Lightname"
transpc = "half"
displace = "bump"
[[item.part]]
partname = "Drops"
partlink = "active"
[[item.part]]
partname = "Wave"
partlink = "deferred"
demo.go
package main
import (
"fmt"
"github.com/BurntSushi/toml"
)
type item struct {
ItemName string
ItemDesc string
Attributes attributes
Part []part
}
type part struct {
PartName string
PartLink string
}
type attributes struct {
Material string
Light string
TransPC string
Displace string
}
type items struct {
Item []item
}
func main() {
var allitems items
if _, err := toml.DecodeFile("demo.toml", &allitems); err != nil {
fmt.Println(err)
return
}
fmt.Printf("\n")
for _, items := range allitems.Item {
fmt.Printf(" Item Name: %s \n", items.ItemName)
fmt.Printf(" Description: %s \n\n", items.ItemDesc)
fmt.Printf(" Material: %s \n", items.Attributes.Material)
fmt.Printf(" Lightmap: %s \n", items.Attributes.Light)
fmt.Printf(" TL Precision: %s \n", items.Attributes.TransPC)
fmt.Printf(" DP Channel: %s \n", items.Attributes.Displace)
fmt.Printf(" Part Name: %s \n", items.Part[0].PartName)
fmt.Printf(" Part Link: %s \n", items.Part[0].PartLink)
# ^
# That's where [:] won't do it.
fmt.Printf("\n────────────────────────────────────────────────────┤\n\n")
}
fmt.Printf("\n")
}
正如评论中所指出的,您需要一个嵌套循环。而不是这个:
fmt.Printf(" Part Name: %s \n", items.Part[0].PartName)
fmt.Printf(" Part Link: %s \n", items.Part[0].PartLink)
使用这个:
for _, part := range items.Part {
fmt.Printf(" Part Name: %s \n", part.PartName)
fmt.Printf(" Part Link: %s \n", part.PartLink)
}
我从 Go 开始,官方 documentation 似乎更适合那些已经了解 Go 并且只想查找资料的人。我希望在这里朝着正确的方向稍微推动一下。
我想要做什么: 使用 BurntSushi 的 TOML 解析器解析 TOML
配置文件具有相同基本特征的元素。
卡住的地方: 我希望每个项目的各自部分与项目一起列出。到目前为止,我只能通过其索引获得其中之一。我正在寻找的是如何以列出相应索引的所有子部分而不是仅列出特定索引的方式进行设置。我可以用 [:]
得到一个 JSON 列表,但似乎无法调整它以获得 正常 输出。
最初我考虑过 [[item.part.001]]
等等,因为它在在线 JSON 解析器中看起来是正确的,但无法弄清楚如何正确地将其读入 Go。由于无论如何我都被卡住了,我对这两种类型都持开放态度,无论哪种方式效果最好。
提前致谢。以下是作为简化的最小工作示例的文件。
demo.toml
# — — — — — — — — — — — — — — — — — — — — — — —
# First Item
# — — — — — — — — — — — — — — — — — — — — — — —
[[item]]
itemname = "Fragments"
itemdesc = "This one can get a bit longer."
[item.attributes]
material = "Basematname"
light = "Lightname"
transpc = "full"
displace = "height"
[[item.part]]
partname = "Shard"
partlink = "active"
[[item.part]]
partname = "Tear"
partlink = "deferred"
[[item.part]]
partname = "crater"
partlink = "disabled"
# — — — — — — — — — — — — — — — — — — — — — — —
# Second Item
# — — — — — — — — — — — — — — — — — — — — — — —
[[item]]
itemname = "Splash"
itemdesc = "This one also can get a bit longer."
[item.attributes]
material = "Other Basematname"
light = "Other Lightname"
transpc = "half"
displace = "bump"
[[item.part]]
partname = "Drops"
partlink = "active"
[[item.part]]
partname = "Wave"
partlink = "deferred"
demo.go
package main
import (
"fmt"
"github.com/BurntSushi/toml"
)
type item struct {
ItemName string
ItemDesc string
Attributes attributes
Part []part
}
type part struct {
PartName string
PartLink string
}
type attributes struct {
Material string
Light string
TransPC string
Displace string
}
type items struct {
Item []item
}
func main() {
var allitems items
if _, err := toml.DecodeFile("demo.toml", &allitems); err != nil {
fmt.Println(err)
return
}
fmt.Printf("\n")
for _, items := range allitems.Item {
fmt.Printf(" Item Name: %s \n", items.ItemName)
fmt.Printf(" Description: %s \n\n", items.ItemDesc)
fmt.Printf(" Material: %s \n", items.Attributes.Material)
fmt.Printf(" Lightmap: %s \n", items.Attributes.Light)
fmt.Printf(" TL Precision: %s \n", items.Attributes.TransPC)
fmt.Printf(" DP Channel: %s \n", items.Attributes.Displace)
fmt.Printf(" Part Name: %s \n", items.Part[0].PartName)
fmt.Printf(" Part Link: %s \n", items.Part[0].PartLink)
# ^
# That's where [:] won't do it.
fmt.Printf("\n────────────────────────────────────────────────────┤\n\n")
}
fmt.Printf("\n")
}
正如评论中所指出的,您需要一个嵌套循环。而不是这个:
fmt.Printf(" Part Name: %s \n", items.Part[0].PartName)
fmt.Printf(" Part Link: %s \n", items.Part[0].PartLink)
使用这个:
for _, part := range items.Part {
fmt.Printf(" Part Name: %s \n", part.PartName)
fmt.Printf(" Part Link: %s \n", part.PartLink)
}