从Golang API数据值中获取你想要的值。

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

Get only the value you want from a Golang API data value

问题

我正在使用Golang制作一个Discord机器人。然而,当我调用API来获取所需的数据值时,出现了错误。API的格式是{"id":"","name":"","level":0 ...}。我想在这里获取level和id的值。

req, err := http.NewRequest("GET", "https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+a, nil)
if err != nil {
   panic(err)
}

req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
req.Header.Add("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5")
req.Header.Add("Origin", "https://developer.riotgames.com")
req.Header.Add("X-Riot-Token", "RGAPI-195db001-c13f-4412-a9ee-bda6e13baa48")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
   panic(err)
}
defer resp.Body.Close()

bytes, _ := ioutil.ReadAll(resp.Body)
str := string(bytes)
fmt.Println(str)

return str
英文:

I'm making a discord bot using Golang.
However, I tried to get only the desired data value by calling the API, but an error occurred. The API format is {"id":"", "name":"", "level":0 ...}. I want to get the values of level and id here.

req, err := http.NewRequest("GET", "https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/"+a, nil)
if err != nil {
   panic(err)
}

req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
req.Header.Add("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5")
req.Header.Add("Origin", "https://developer.riotgames.com")
req.Header.Add("X-Riot-Token", "RGAPI-195db001-c13f-4412-a9ee-bda6e13baa48")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
   panic(err)
}
defer resp.Body.Close()

bytes, _ := ioutil.ReadAll(resp.Body)
str := string(bytes)
fmt.Println(str)

return str

答案1

得分: 2

尝试构建一个具有预期字段的结构体,并尝试将响应与结构体绑定。

在这里查看:其他堆栈溢出绑定主题

type myStruct struct {
	Id    string `json:"id"`
	Level string `json:"level"`
}
func foo() myStruct {
    var ms myStruct
	req, err := http.NewRequest("GET", "https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/", nil)
	if err != nil {
		log.Printf("error: %s", err)
	}

	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
	req.Header.Add("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5")
	req.Header.Add("Origin", "https://developer.riotgames.com")
	req.Header.Add("X-Riot-Token", "RGAPI-195db001-c13f-4412-a9ee-bda6e13baa48")

	var client http.Client
	resp, err := client.Do(req)
	if err != nil {
		log.Printf("error: %s", err)
	}
	defer resp.Body.Close()

	json.NewDecoder(resp.Body).Decode(&ms)

	return ms
}

由于得到403错误,无法正确测试它。此外,最好检查响应状态码。使用panic处理错误不是最佳方式,因为你尝试访问的服务器可能已经关闭。

英文:

Try to build a struct with expected fields and try to bind response with struct.

check here: other stack overflow binding topic

type myStruct struct {
	Id    string `json:"id"`
	Level string `json:"level"`
}
func foo () myStruct{
    var ms myStruct
	req, err := http.NewRequest("GET", "https://kr.api.riotgames.com/lol/summoner/v4/summoners/by-name/", nil)
	if err != nil {
		log.Printf("error: %s", err)
	}

	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
	req.Header.Add("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,ja-JP;q=0.6,ja;q=0.5")
	req.Header.Add("Origin", "https://developer.riotgames.com")
	req.Header.Add("X-Riot-Token", "RGAPI-195db001-c13f-4412-a9ee-bda6e13baa48")

	var client http.Client
	resp, err := client.Do(req)
	if err != nil {
		log.Printf("error: %s", err)
	}
	defer resp.Body.Close()

	json.NewDecoder(resp.Body).Decode(&ms)

	return ms
}

can't test it properly because got 403. Also it will be good to check response status code. Also panic it's not a best way to handle and err. Server you try to reach could be down.

huangapple
  • 本文由 发表于 2022年12月20日 15:30:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/74859749.html
匿名

发表评论

匿名网友

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

确定