drrvnbwle80177811
2017-12-18 00:53
浏览 646
已采纳

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

I'm starting with Go and the official documentation seems more for people who know Go already and just want to look stuff up. I'm hoping for a little nudge in the right direction here.

What I'm looking to do: Parse a TOML config file with BurntSushi's TOML parser that consists of several elements sharing the same base characteristics.

Where I'm stuck: I want each item's respective parts to be listed with the item. So far I can only get one of them by its index. What I'm looking for is how to set it up in a way that lists all sub parts of the respective index instead of just a specific one. I can get a JSON list with [:] but can't seem to adapt that to get normal output.

Initially I had considered [[item.part.001]] and so on because it looked right in the online JSON parsers, but wasn't able to figure out how to read that properly into Go. Since I'm stuck anyway, I'm open to both types, whatever works best.

Thanks in advance. Here are the files as an abbreviated minimum working example.


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("
")

    for _, items := range allitems.Item {
        fmt.Printf("    Item Name: %s 
", items.ItemName)
        fmt.Printf("  Description: %s 

", items.ItemDesc)

        fmt.Printf("     Material: %s 
", items.Attributes.Material)
        fmt.Printf("     Lightmap: %s 
", items.Attributes.Light)
        fmt.Printf(" TL Precision: %s 
", items.Attributes.TransPC)
        fmt.Printf("   DP Channel: %s 
", items.Attributes.Displace)


        fmt.Printf("    Part Name: %s 
", items.Part[0].PartName)
        fmt.Printf("    Part Link: %s 
", items.Part[0].PartLink)
        #                                             ^
        #                              That's where [:] won't do it.   

        fmt.Printf("
────────────────────────────────────────────────────┤

")


    }

    fmt.Printf("
")


}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douju2599 2017-12-20 05:54
    已采纳

    As pointed out in the comments, you need a nested loop. Instead of this:

        fmt.Printf("    Part Name: %s 
    ", items.Part[0].PartName)
        fmt.Printf("    Part Link: %s 
    ", items.Part[0].PartLink)
    

    use this:

        for _, part := range items.Part {
            fmt.Printf("    Part Name: %s 
    ", part.PartName)
            fmt.Printf("    Part Link: %s 
    ", part.PartLink)
        }
    
    点赞 评论

相关推荐 更多相似问题