Goã§APIããåå¾ããJSONã5åã§ãã¼ã¹ãã
å¾®å¦ã«é¢åãªã¢ã¬
Goã¯APIã®ç¨éã§ãç§ã¯çµæ§ä½¿ãã®ã§ããããã®APIãæ§ç¯ããä¸ã§ãå¤é¨ã®APIã使ã£ã¦ãã¼ã¿ãéããããããã¨ãããããã¾ããããã¦é¿ãã¦éããªãã®ããJSON解æã§ããèªåã§ããã¨å°å³ã«é¢åã§ãã
ä»åã¯ãã®ä½æ¥ã¯5åã§çµããããæ¹æ³ãç´¹ä»ãã¾ãã
ã¯ãã½ã¼ã¹
便å©ãªãã¼ã«é
-
https://mholt.github.io/json-to-go/JSON-to-Go: Convert JSON to Go instantly
JSONãé£ãããã¨ã解æããstructãçæãã¦ãããã JSON Pretty Linter - JSONの整形と構文チェック
JSONãæ´å½¢ãã¦ãããã
Let’s try!!!
ç®çã®JSONãåå¾ãã
connpass.com ä»åã¯Connpassã®APIããè¿ã£ã¦ããJSONã解æãã¾ãã
{ "results_returned": 1, "events": [ { "event_url": "https://kikaigakushuu.connpass.com/event/56040/", "event_type": "participation", "owner_nickname": "keiichirou_miyamoto", "series": { "url": "https://kikaigakushuu.connpass.com/", "id": 2589, "title": "人工ç¥è½åå¼·ä¼" }, "updated_at": "2017-04-26T02:47:01+09:00", "lat": "35.696575200000", "started_at": "2017-05-09T20:00:00+09:00", "hash_tag": "geek", "title": "å¼·ããªããããã£ãã¯ã»ã²ã¼ã ãã¬ã¤ã¤ã¼ã®ä½ãæ¹åå¼·ä¼ã4ç« å¼·åå¦ç¿ ååã", "event_id": 56040, "lon": "139.771830100000", "waiting": 0, "limit": 30, "owner_id": 10866, "owner_display_name": "keiichirou_miyamoto", "description": "説æ", "address": "æ±äº¬é½å代ç°åºç¥ç°é ç°çºï¼ä¸ç®ï¼ï¼âï¼ï¼ (éæ第ï¼ãã«ï¼é)", "catch": "#人工ç¥è½,#æ©æ¢°å¦ç¿,#深層å¦ç¿,#ãã¥ã¼ã©ã«ããã,#ãã£ã¼ãã©ã¼ãã³ã°,#åå¿è ", "accepted": 17, "ended_at": "2017-05-09T22:00:00+09:00", "place": "ã³ã¯ã¼ãã³ã°ã¹ãã¼ã¹ç§èå Weeybleï¼ã¦ã£ã¼ãã«)" } ], "results_start": 1, "results_available": 1744 }
https://connpass.com/api/v1/event/?keyword=python&count=1
ãªã¯ã¨ã¹ãããã©ã¦ã¶ã§å®è¡ãã¾ãã
JSON Pretty Linter - JSONの整形と構文チェック ã³ãã¼ãã¦ãJSONæ´å½¢ãã¾ãã æ´å½¢ããJSONãã³ãã¼ãã¦ãgoã®ã½ã¼ã¹ã«è²¼ãä»ãã¾ãã
jsonStrã¨ããå¤æ°ã«ä»¥ä¸ã®ãããªæãã§ä»£å ¥ãã¦ãã ããã
var jsonStr = `{ "results_returned": 1, "events": [ { "event_url": "https://kikaigakushuu.connpass.com/event/56040/", "event_type": "participation", "owner_nickname": "keiichirou_miyamoto", "series": { "url": "https://kikaigakushuu.connpass.com/", "id": 2589, "title": "人工ç¥è½åå¼·ä¼" }, "updated_at": "2017-04-26T02:47:01+09:00", "lat": "35.696575200000", "started_at": "2017-05-09T20:00:00+09:00", "hash_tag": "geek", "title": "å¼·ããªããããã£ãã¯ã»ã²ã¼ã ãã¬ã¤ã¤ã¼ã®ä½ãæ¹åå¼·ä¼ã4ç« å¼·åå¦ç¿ ååã", "event_id": 56040, "lon": "139.771830100000", "waiting": 0, "limit": 30, "owner_id": 10866, "owner_display_name": "keiichirou_miyamoto", "description": "説æ", "address": "æ±äº¬é½å代ç°åºç¥ç°é ç°çºï¼ä¸ç®ï¼ï¼âï¼ï¼ (éæ第ï¼ãã«ï¼é)", "catch": "#人工ç¥è½,#æ©æ¢°å¦ç¿,#深層å¦ç¿,#ãã¥ã¼ã©ã«ããã,#ãã£ã¼ãã©ã¼ãã³ã°,#åå¿è ", "accepted": 17, "ended_at": "2017-05-09T22:00:00+09:00", "place": "ã³ã¯ã¼ãã³ã°ã¹ãã¼ã¹ç§èå Weeybleï¼ã¦ã£ã¼ãã«)" } ], "results_start": 1, "results_available": 1744 }`
JSONããstructãä½æãã
https://mholt.github.io/json-to-go/JSON-to-Go: Convert JSON to Go instantly
jsonãé£ããã¦ãstrcutãçæããã
å³ã«åºã¦ãstrcutãã³ãã¼ãã¦goã®ã½ã¼ã¹ã«è²¼ãä»ãã
type AutoGenerated struct { ResultsReturned int `json:"results_returned"` Events []struct { EventURL string `json:"event_url"` EventType string `json:"event_type"` OwnerNickname string `json:"owner_nickname"` Series struct { URL string `json:"url"` ID int `json:"id"` Title string `json:"title"` } `json:"series"` UpdatedAt time.Time `json:"updated_at"` Lat string `json:"lat"` StartedAt time.Time `json:"started_at"` HashTag string `json:"hash_tag"` Title string `json:"title"` EventID int `json:"event_id"` Lon string `json:"lon"` Waiting int `json:"waiting"` Limit int `json:"limit"` OwnerID int `json:"owner_id"` OwnerDisplayName string `json:"owner_display_name"` Description string `json:"description"` Address string `json:"address"` Catch string `json:"catch"` Accepted int `json:"accepted"` EndedAt time.Time `json:"ended_at"` Place string `json:"place"` } `json:"events"` ResultsStart int `json:"results_start"` ResultsAvailable int `json:"results_available"` }
Umarshalã§JSONããã¼ã¹ãã
jsonããã±ã¼ã¸ã®Unmarshalã§ãã¼ã¹ãã¦çµäºã
jsonBytes := ([]byte)(jsonStr) data := new(AutoGenerated) if err := json.Unmarshal(jsonBytes, data); err != nil { fmt.Println("JSON Unmarshal error:", err) return } fmt.Println(data.Events[0])
$ go run main.go
以ä¸ã§ãå®äºã§ãï¼ï¼ï¼ç°¡åãããï¼ï¼ï¼ï¼
å ¨ã½ã¼ã¹
package main import ( "encoding/json" "fmt" "time" ) type AutoGenerated struct { ResultsReturned int `json:"results_returned"` Events []struct { EventURL string `json:"event_url"` EventType string `json:"event_type"` OwnerNickname string `json:"owner_nickname"` Series struct { URL string `json:"url"` ID int `json:"id"` Title string `json:"title"` } `json:"series"` UpdatedAt time.Time `json:"updated_at"` Lat string `json:"lat"` StartedAt time.Time `json:"started_at"` HashTag string `json:"hash_tag"` Title string `json:"title"` EventID int `json:"event_id"` Lon string `json:"lon"` Waiting int `json:"waiting"` Limit int `json:"limit"` OwnerID int `json:"owner_id"` OwnerDisplayName string `json:"owner_display_name"` Description string `json:"description"` Address string `json:"address"` Catch string `json:"catch"` Accepted int `json:"accepted"` EndedAt time.Time `json:"ended_at"` Place string `json:"place"` } `json:"events"` ResultsStart int `json:"results_start"` ResultsAvailable int `json:"results_available"` } func main() { var jsonStr = `{ "results_returned": 1, "events": [ { "event_url": "https://kikaigakushuu.connpass.com/event/56040/", "event_type": "participation", "owner_nickname": "keiichirou_miyamoto", "series": { "url": "https://kikaigakushuu.connpass.com/", "id": 2589, "title": "人工ç¥è½åå¼·ä¼" }, "updated_at": "2017-04-26T02:47:01+09:00", "lat": "35.696575200000", "started_at": "2017-05-09T20:00:00+09:00", "hash_tag": "geek", "title": "å¼·ããªããããã£ãã¯ã»ã²ã¼ã ãã¬ã¤ã¤ã¼ã®ä½ãæ¹åå¼·ä¼ã4ç« å¼·åå¦ç¿ ååã", "event_id": 56040, "lon": "139.771830100000", "waiting": 0, "limit": 30, "owner_id": 10866, "owner_display_name": "keiichirou_miyamoto", "description": "説æ", "address": "æ±äº¬é½å代ç°åºç¥ç°é ç°çºï¼ä¸ç®ï¼ï¼âï¼ï¼ (éæ第ï¼ãã«ï¼é)", "catch": "#人工ç¥è½,#æ©æ¢°å¦ç¿,#深層å¦ç¿,#ãã¥ã¼ã©ã«ããã,#ãã£ã¼ãã©ã¼ãã³ã°,#åå¿è ", "accepted": 17, "ended_at": "2017-05-09T22:00:00+09:00", "place": "ã³ã¯ã¼ãã³ã°ã¹ãã¼ã¹ç§èå Weeybleï¼ã¦ã£ã¼ãã«)" } ], "results_start": 1, "results_available": 1744 }` jsonBytes := ([]byte)(jsonStr) data := new(AutoGenerated) if err := json.Unmarshal(jsonBytes, data); err != nil { fmt.Println("JSON Unmarshal error:", err) return } fmt.Println(data.Events[0]) }
å®éã«APIã«ãªã¯ã¨ã¹ããã
package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" "time" ) type AutoGenerated struct { ResultsReturned int `json:"results_returned"` Events []struct { EventURL string `json:"event_url"` EventType string `json:"event_type"` OwnerNickname string `json:"owner_nickname"` Series struct { URL string `json:"url"` ID int `json:"id"` Title string `json:"title"` } `json:"series"` UpdatedAt time.Time `json:"updated_at"` Lat string `json:"lat"` StartedAt time.Time `json:"started_at"` HashTag string `json:"hash_tag"` Title string `json:"title"` EventID int `json:"event_id"` Lon string `json:"lon"` Waiting int `json:"waiting"` Limit int `json:"limit"` OwnerID int `json:"owner_id"` OwnerDisplayName string `json:"owner_display_name"` Description string `json:"description"` Address string `json:"address"` Catch string `json:"catch"` Accepted int `json:"accepted"` EndedAt time.Time `json:"ended_at"` Place string `json:"place"` } `json:"events"` ResultsStart int `json:"results_start"` ResultsAvailable int `json:"results_available"` } func main() { url := "https://connpass.com/api/v1/event/?keyword=python&count=1" resp, _ := http.Get(url) defer resp.Body.Close() byteArray, _ := ioutil.ReadAll(resp.Body) jsonBytes := ([]byte)(byteArray) data := new(AutoGenerated) if err := json.Unmarshal(jsonBytes, data); err != nil { fmt.Println("JSON Unmarshal error:", err) return } fmt.Println(data.Events[0]) }
以ä¸ã§çµäºã§ããGoæé«ã£ããã