Filter JSON returned from GitHub API via Go

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

Filter JSON returned from GitHub API via Go

问题

我正在尝试从GitHub存储库中获取最新的发布信息,并获取该发布中的特定资源。以下代码打印出发布标签和Asset结构下的所有资源。我想要提取特定项及其下载URL,比如只提取ajour.exe标签。我可以通过结构体来实现这个吗,还是应该解析输出来获取它?

func GetGithubAsset() {
    testUri := "https://api.github.com/repos/ajour/ajour/releases/latest"
    type githubApiData struct {
       AppVersion string `json:"tag_name"`
       Assets     []struct {
          Name               string `json:"name"`
          BrowserDownloadURL string `json:"browser_download_url"`
       }
    }
    resp, err := http.Get(testUri)
    if err != nil {
		log.Fatal(err)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	var data githubApiData
	jsonErr := json.Unmarshal(body, &data)
	if jsonErr != nil {
		log.Fatal(jsonErr)
	}
	fmt.Println(data.AppVersion)
	fmt.Println(data.Assets)
}
英文:

I'm trying to get the latest release information from a github repository and grab a specific asset in that release. The following code prints out the release tag and the all the assets under the Asset structure. I'd like to be able to pull out a specific item and its download url, such as just the ajour.exe tag. Can I do that via the struct, or should I be parsing the output to grab it?

func GetGithubAsset() {
    testUri := "https://api.github.com/repos/ajour/ajour/releases/latest"
    type githubApiData struct {
       AppVersion string `json:"tag_name"`
       Assets     []struct {
          Name               string `json:"name"`
          BrowserDownloadURL string `json:"browser_download_url"`
       }
    }
    resp, err := http.Get(testUri)
    if err != nil {
		log.Fatal(err)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	var data githubApiData
	jsonErr := json.Unmarshal(body, &data)
	if jsonErr != nil {
		log.Fatal(jsonErr)
	}
	fmt.Println(data.AppVersion)
	fmt.Println(data.Assets)
}

答案1

得分: 2

你快要完成了。请注意,json.Unmarshal会为你解析输出。你只需要循环遍历Assets字段,类似于以下方式(替换fmt.Println(data.Assets)的部分):

    for _, asset := range data.Assets {
        if asset.Name == "ajour.exe" {
            fmt.Println(asset.BrowserDownloadURL)
        }
    }
英文:

You're almost there. Note that the json.Unmarshal does parse the output for you. All you need to is loop through the Assets field, something like so (in place of fmt.Println(data.Assets)):

    for _, asset := range data.Assets {
        if asset.Name == "ajour.exe" {
            fmt.Println(asset.BrowserDownloadURL)
        }
    }

答案2

得分: -1

json标签json:"assets"缺失

func GetGithubAsset() {
    testUri := "https://api.github.com/repos/ajour/ajour/releases/latest"
    type githubApiData struct {
        AppVersion string `json:"tag_name"`
        Assets     []struct {
            Name               string `json:"name"`
            BrowserDownloadURL string `json:"browser_download_url"`
        } `json:"assets"`
    }
    resp, err := http.Get(testUri)
    if err != nil {
        log.Fatal(err)
    }
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }
    var data githubApiData
    jsonErr := json.Unmarshal(body, &data)
    if jsonErr != nil {
        log.Fatal(jsonErr)
    }
    fmt.Println(data.AppVersion)
    fmt.Println(data.Assets)
}
英文:

The json tag json:"assets" is missing

func GetGithubAsset() {
	testUri := "https://api.github.com/repos/ajour/ajour/releases/latest"
	type githubApiData struct {
		AppVersion string `json:"tag_name"`
		Assets     []struct {
			Name               string `json:"name"`
			BrowserDownloadURL string `json:"browser_download_url"`
		} `json:"assets"`
	}
	resp, err := http.Get(testUri)
	if err != nil {
		log.Fatal(err)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	var data githubApiData
	jsonErr := json.Unmarshal(body, &data)
	if jsonErr != nil {
		log.Fatal(jsonErr)
	}
	fmt.Println(data.AppVersion)
	fmt.Println(data.Assets)
}

huangapple
  • 本文由 发表于 2022年6月1日 09:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/72455525.html
匿名

发表评论

匿名网友

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

确定