Golang中的结构体数组的JSON对象

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

Golang Struct an Array of Json objects

问题

我有点困惑为什么我的方法不起作用。
我试图实现的目标是从返回 JSON 格式的 API 调用中获取 ID。然后,我需要通过传递该 ID 进行另一个调用,以获取该调用中的所有值。
下面是我的方法和我正在尝试的大致布局。

res, err := http.Get("xyz")
if err != nil {
    log.Fatalln(err)
}
defer res.Body.Close()

var publications []Publications
body, err := ioutil.ReadAll(res.Body)
if err != nil {
    log.Fatalln(err)
}

json.Unmarshal(body, &publications)

fmt.Println(publications)

我目前有一个类似于以下内容的 JSON 数组,这是第一个 API 调用的返回结果:

[
    {
        "id": 12101,
        "location": "SD",
        "name": "Joe",
        "date": "2022-04-21T00:00:00-04:00"
    },
    {
        "id": 141201,
        "location": "NY",
        "name": "Sky",
        "date": "2022-04-21T00:00:00-04:00"
    }
]
英文:

I'm a bit confused as to why my method of approach isn't working.
What i'm trying to accomplish is to obtain the ID from the api call that returns the json format. I would then need to subsequently do another call by passing in that id to obtain the values for everything within that call
Below you'll see my method of approach and the rough layout of what i'm playing with.

	res, err := http.Get("xyz")
	if err != nil {
		log.Fatalln(err)
	}
	defer res.Body.Close()

	var publications []Publications
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatalln(err)
	}

	json.Unmarshal(body, &publications)

	fmt.Println((publications))

I currently have a Json array similar to this First api Call

[
    {
      "id": 12101,
      "location":"SD",
      "name": "Joe",
      "date": "2022-04-21T00:00:00-04:00",
    },
    {
      "id": 141201,
      "location": "NY",
      "name": "Sky",
      "date": "2022-04-21T00:00:00-04:00",
    },
]

答案1

得分: 3

这个示例展示了如何将第一个 JSON 数组解析为结构体。

playground 上试玩一下。

package main

import (
	"encoding/json"
	"fmt"
	"time"
)

type payload []entry

type entry struct {
	Id       int       `json:"id"`
	Location string    `json:"location"`
	Name     string    `json:"name"`
	Date     time.Time `json:"date"`
}

func main() {

	str := `
[
    {
      "id": 12101,
      "location":"SD",
      "name": "Joe",
      "date": "2022-04-21T00:00:00-04:00"
    },
    {
      "id": 141201,
      "location": "NY",
      "name": "Sky",
      "date": "2022-04-21T00:00:00-04:00"
    }
]
`

	var publication1 payload
	err := json.Unmarshal([]byte(str), &publication1)
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}

	for _, p := range publication1 {
		fmt.Printf("id = %d\n", p.Id)
	}
}

输出:

id = 12101
id = 141201
英文:

This example shows how to parse your first JSON array into a struct.

Play with it on the playground.

package main

import (
	"encoding/json"
	"fmt"
	"time"
)

type payload []entry

type entry struct {
	Id       int       `json:"id"`
	Location string    `json:"location"`
	Name     string    `json:"name"`
	Date     time.Time `json:"date"`
}

func main() {

	str := `
[
    {
      "id": 12101,
      "location":"SD",
      "name": "Joe",
      "date": "2022-04-21T00:00:00-04:00"
    },
    {
      "id": 141201,
      "location": "NY",
      "name": "Sky",
      "date": "2022-04-21T00:00:00-04:00"
    }
]
`

	var publication1 payload
	err := json.Unmarshal([]byte(str), &publication1)
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}

	for _, p := range publication1 {
		fmt.Printf("id = %d\n", p.Id)
	}
}

Output:

id = 12101
id = 141201

huangapple
  • 本文由 发表于 2022年5月1日 12:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/72074033.html
匿名

发表评论

匿名网友

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

确定