나는, 그러나Golang : json 배열 API 응답을 구문 분석/비 정렬 화/해독하는 방법은 무엇입니까?
카운트 내가 이것을 달성하기 위해 구현하기 위해 노력했다 코드를 내가보기에서 인쇄를 진행합니다있는 구조체의 배열로 https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/all-agents/Smithsonian_Institution/daily/20160101/20170101
에있는 위키 백과의 API의 응답을 분석하는 것을 시도하고있다 터미널을 빌드하고 실행할 때 터미널에서 아무 것도 반환하지 않습니까?
내가 성공에 실패한 코드는 다음과 같습니다. 그런 다음, 데이터 스토어에 삽입하거나 따라서 인쇄 할 구조체의 어레이에 해당 배열 파싱 상기 후 언급 한 바와 같이 API로부터 JSON 응답을 수신하는 최적의 방법은 무엇
type Post struct {
Project string `json:"project"`
Article string `json:"article"`
Granularity string `json:"granularity"`
Timestamp string `json:"timestamp"`
Access string `json:"access"`
Agent string `json:"agent"`
Views int `json:"views"`
}
func main(){
//The name of the wikipedia post
postName := "Smithsonian_Institution"
//The frequency of
period := "daily"
//When to start the selection
startDate := "20160101"
//When to end the selection
endDate := "20170101"
url := fmt.Sprintf("https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia.org/all-access/all-agents/%s/%s/%s/%s", postName, period, startDate, endDate)
//Get from URL
req, err := http.Get(url)
if err != nil{
return
}
defer req.Body.Close()
var posts []Post
body, err := ioutil.ReadAll(req.Body)
if err != nil {
panic(err.Error())
}
json.Unmarshal(body, &posts)
// Loop over structs and display the respective views.
for p := range posts {
fmt.Printf("Views = %v", posts[p].Views)
fmt.Println()
}
}
.
감사합니다.
답변 해 주셔서 감사합니다. 완벽하게 작동했습니다! –