从文件中解析嵌套的 JSON 数组。

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

Parse nested json array from a file

问题

我有一个名为sample.json的JSON文件,其中包含以下JSON数组:

[
{
  "time": "2021-01-04T00:11:32.362Z",
  "extra_data": {
    "id": "123"
  },
  "info": "event123"
},
{
  "time": "2021-01-05T00:11:32.362Z",
  "extra_data": {
    "id": "456"
  },
  "info": "event456"
},
{
  "time": "2021-01-06T00:11:32.362Z",
  "extra_data": {
    "id": "789"
  },
  "info": "event789"
}
]

我想要解组这个JSON数组,以便对于每个JSON对象(即事件),我可以访问idinfo的值。这是我目前的代码:

func main() {
    file, err := ioutil.ReadFile("/Users/janedoe/Downloads/sample.json")
    var events []Event
    json.Unmarshal([]byte(file), &events)
    fmt.Println(reflect.TypeOf(events))
    // 为每个事件按其键读取每个值
    fmt.Println("Event123_Time :", events[0].Time,
            "\nEvent123_ExtraData_Id :", events[0].ExtraData.Id,
            "\nEvent123_Info :", events[0].Info)
} 

type Event struct {
    Time string `json:"time"`
    ExtraData ExtraData `json:"extra_data"`
    Info string `json:"info"`
}

type ExtraData struct {
     Id string   `json:"id"`
}

我得到的输出是:

[]main.Event
Event123_Time :  
Event123_ExtraData_Id :
Event123_Info : 

我没有得到任何值,这意味着解组没有按预期进行。我该如何修复这个问题?

英文:

I have a json file sample.json containing a json array as follows:

[
{
  "time": "2021-01-04T00:11:32.362Z",
  "extra_data": {
    "id": "123"
  },
  "info": "event123"
},
{
  "time": "2021-01-05T00:11:32.362Z",
  "extra_data": {
    "id": "456"
  },
  "info": "event456"
},
{
  "time": "2021-01-06T00:11:32.362Z",
  "extra_data": {
    "id": "789"
  },
  "info": "event789"
}
]

I am trying to unmarshal this json array so that for each json object (i.e. event), I can access the id and info values. This is what I have so far:

func main() {

    file, err := ioutil.ReadFile("/Users/janedoe/Downloads/sample.json")
    var events Event
    json.Unmarshal([]byte(file), &events)
    fmt.Println(reflect.TypeOf(events))
    // Reading each value by its key for each event
    fmt.Println("Event123_Time :", events.Timestamp,
            "\nEvent123_ExtraData_Id :", events.ExtraData.Id,
            "\nEvent123_Info :", events.Info)
} 
type Event struct {
    Time string `json:"time"`
    ExtraData ExtraData `json:"extra_data"`
    Info string `json:"info"`
}

type ExtraData struct {
     Id string   `json:"id"`
}

The output that I am getting is:

main.Event
Event123_Time :  
Event123_ExtraData_Id :
Event123_Info : 

I am not getting any values, implying that the marshalling is not happening as expected. How can I fix this?

答案1

得分: 2

由于您的JSON是一个数组,我认为您想要:

var events []Event

而不是

var events Event

以下是已经进行了更改的版本:

func main() {
	file, err := ioutil.ReadFile("/Users/janedoe/Downloads/sample.json")
	if err != nil {
		panic(err)
	}
	var events []Event
	if err := json.Unmarshal([]byte(file), &events); err != nil {
		panic(err)
	}
	fmt.Println(reflect.TypeOf(events))
	for i, event := range events {
		// 为每个事件按其键读取每个值
		fmt.Println(i, "Event123_Time :", event.Time,
			"\nEvent123_ExtraData_Id :", event.ExtraData.Id,
			"\nEvent123_Info :", event.Info)
	}
}

type Event struct {
	Time      string    `json:"time"`
	ExtraData ExtraData `json:"extra_data"`
	Info      string    `json:"info"`
}

type ExtraData struct {
	Id string `json:"id"`
}

希望对您有所帮助!

英文:

Since your json is an array, I think you want:

var events []Event

not

var events Event

Here's a version with that change:

func main() {
	file, err := ioutil.ReadFile("/Users/janedoe/Downloads/sample.json")
	if err != nil {
		panic(err)
	}
	var events []Event
	if err := json.Unmarshal([]byte(file), &events); err != nil {
		panic(err)
	}
	fmt.Println(reflect.TypeOf(events))
	for i, event := range events {
		// Reading each value by its key for each event
		fmt.Println(i, "Event123_Time :", event.Time,
			"\nEvent123_ExtraData_Id :", event.ExtraData.Id,
			"\nEvent123_Info :", event.Info)
	}
}

type Event struct {
	Time      string    `json:"time"`
	ExtraData ExtraData `json:"extra_data"`
	Info      string    `json:"info"`
}

type ExtraData struct {
	Id string `json:"id"`
}

huangapple
  • 本文由 发表于 2021年12月31日 09:02:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/70538103.html
匿名

发表评论

匿名网友

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

确定