如何在TOML中解析嵌套数组/子表?

问题描述 投票:0回答:1

我从Go开始,官方的documentation似乎更适合那些已经知道Go并且只是想看看东西的人。我希望在这里向正确的方向稍微推动一下。

我要做的事情:使用BurntSushi的TOML解析器解析TOML配置文件,该解析器由几个共享相同基本特征的元素组成。

我被卡住的地方:我希望每个项目的各个部分都与项目一起列出。到目前为止,我只能通过其索引获得其中一个。我正在寻找的是如何以列出相应索引的所有子部分而不仅仅是特定索引的方式进行设置。我可以用[:]获得一个JSON列表,但似乎无法适应它以获得正常输出。

最初我考虑过[[item.part.001]]等等,因为它在在线JSON解析器中看起来是正确的,但是无法弄清楚如何正确地读入Go。因为无论如何我都被卡住了,我对两种类型都持开放态度,无论哪种效果最好。

提前致谢。以下是文件作为缩写的最小工作示例。


但是。和毫升

# — — — — —  — — — — — — — — — — — — — — — — — — 
#                   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")


}
go struct toml
1个回答
0
投票

正如评论中指出的那样,您需要一个嵌套循环。而不是这个:

    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)
    }
© www.soinside.com 2019 - 2024. All rights reserved.