英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论