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

如何解析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)
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图