使用Go访问嵌套数组和对象中的数据

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

Accessing data in nested arrays & objects with Go

问题

我正在尽力将一些JSON数据解组成可用的形式,但似乎无法成功。数据如下:

{
"series": [
{
"series_id": "PET.EMD_EPD2D_PTE_NUS_DPG.W",
"name": "U.S. No 2 Diesel Retail Prices, Weekly",
"units": "Dollars per Gallon",
"updated": "2013-09-27T07:21:57-0400",
"data": [
[
"20130923",
"3.949"
],
[
"20130916",
"3.974"
]
]
}
]
}

我试图将data下的数组存入一个变量中,以便可以遍历它们并执行类似以下的操作:

if data[i][0] == "20130923" {
fuelPrice.Price == data[i][1]
}

我尝试将数据解组成一个结构体,但无法解决series的问题...也就是说,我无法解决如何处理嵌套数组的问题。以下类似的尝试都不起作用:

type Series struct {
SeriesId string
Name string
Data [][]string
}

type RawFuelPrice struct {
Series []Series
Data []interface{}[]
}

另外,如果我解组成interface{},我也不知道如何访问数据...

我肯定是个初学者。感谢你的时间和努力。

英文:

I'm doing my best to unmarshall some json data into usable form in Go, but can't seem to get it. The data is:

{
    "series": [
        {
            "series_id": "PET.EMD_EPD2D_PTE_NUS_DPG.W",
            "name": "U.S. No 2 Diesel Retail Prices, Weekly",
            "units": "Dollars per Gallon",
            "updated": "2013-09-27T07:21:57-0400",
            "data": [
                [
                    "20130923",
                    "3.949"
                ],
                [
                    "20130916",
                    "3.974"
                ]
            ]
        }
    ]
}

I'm trying to get the arrays under data into a variable, so I can loop through them and do something like:

if data[i][0] == "20130923" {
    fuelPrice.Price == data[i][1]
}

I've tried to unmarshall the data into a struct but I can't figure out how to get past series... ie I can't figure out how to do nested arrays. Things like these don't work:

type Series struct {
    SeriesId    string
    Name        string
    Data        [][]string
}

type RawFuelPrice struct {
    Series []Series
    Data []interface{}[]
}

Also if I unmarshal into an interface{}, I can't figure out how to access the data...

I'm definitely a beginner. Thanks for your time and effort.

答案1

得分: 1

你的代码没问题,除了RawFuelPrice结构体的Data成员。我认为那个语法是无效的,并且在JSON数据块的顶层没有Data属性。

不过,你可以这样提取数据:

var rfp RawFuelPrice
json.Unmarshal(input, &rfp)
for _, s := range rfp.Series {
    fmt.Println("Name", s.Name)
    for _, d := range s.Data {
        fmt.Println("\tdate:", d[0])
        fmt.Println("\tprice:", d[1])
    }
    fmt.Println()
}

不过,你可能需要检查所有的数据是否都存在。

Go Playground链接:http://play.golang.org/p/C47lZJ_L0o

英文:

Your code is just fine- except for the Data member of the RawFuelPrice struct. I don't think that syntax is valid, and there's no Data attribute at the top level of of the JSON blob.

That said, this is how you'd get the data out:

var rfp RawFuelPrice
json.Unmarshal(input, &rfp)
for _,s := range rfp.Series {
	fmt.Println("Name",s.Name)
	for _,d := range s.Data {
		fmt.Println("\tdate:",d[0])
		fmt.Println("\tprice:",d[1])
	}
	fmt.Println()
}

Although you'd probably want to check that all the data was there.

Go Playground link: http://play.golang.org/p/C47lZJ_L0o

huangapple
  • 本文由 发表于 2013年10月3日 04:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/19146474.html
匿名

发表评论

匿名网友

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

确定