質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Q&A

解決済

1回答

3763閲覧

Go初心者です。mapの配列?のなかから要素を取り出すことができません。

yutaro1204

総合スコア4

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

0グッド

1クリップ

投稿2019/02/01 08:47

編集2019/02/01 12:45

前提・実現したいこと

Goで現在の天気を返すCUIを作りたいとおもっています。

例:
cmd上で
weather.exe today
と打つと、
Weather in Tokyo: Cloud
みたいな感じで返してきてほしいです。

発生している問題・エラーメッセージ

Goからhttpでhttps://openweathermap.org/からgetしてきた天気の情報セットから上手く自分の欲しいデータを抽出できないでいます。
具体的には、以下の形式のデータを取得することができましたが、その中の要素をうまく取れません。

cmd

1[map[id:801 main:Clouds description:few clouds icon:02d]]

該当のソースコード

go

1package main 2 3import ( 4 "github.com/mitchellh/cli" 5 "os" 6 "io" 7 "fmt" 8 "net/http" 9 "io/ioutil" 10 "encoding/json" 11) 12type TestCommand struct {} 13func (c *TestCommand) Run(args []string) int { 14 fmt.Println("you typed test") 15 resp, _ := http.Get("http://api.openweathermap.org/data/2.5/weather?q=Tokyo,jp&APPID={apikey}") 16 defer resp.Body.Close() 17 byteArray, _ := ioutil.ReadAll(resp.Body) 18 var weather interface{} 19 json.Unmarshal(byteArray, &weather) 20 fmt.Println(weather.(map[string]interface{})["weather"]) // [map[id:801 main:Clouds description:few clouds icon:02d]] 21 return 0 22} 23func (c *TestCommand) Synopsis() string { 24 return "Test" 25} 26func (c *TestCommand) Help() string { 27 return "Usage: isay goodbye" 28} 29func main() { 30 c := cli.NewCLI("isay", "0.0.1") 31 32 c.Args = os.Args[1:] 33 c.Commands = map[string]cli.CommandFactory{ 34 "hello": func() (cli.Command, error) { 35 return &HelloCommand{}, nil 36 }, 37 "goodbye": func() (cli.Command, error) { 38 return &GoodbyeCommand{}, nil 39 }, 40 "test": func() (cli.Command, error) { 41 return &TestCommand{}, nil 42 }, 43 } 44 exitStatus, err := c.Run() 45 if err != nil { 46 fmt.Println(err) 47 } 48 os.Exit(exitStatus) 49}

ちなみに、APIで返ってくるデータは、以下のような形式です。

json

1{ 2"coord": { 3"lon": 139.76, 4"lat": 35.68 5}, 6"weather": [ 7 { 8"id": 801, 9"main": "Clouds", 10"description": "few clouds", 11"icon": "02d" 12} 13], 14"base": "stations", 15"main": { 16"temp": 281.15, 17"pressure": 1010, 18"humidity": 26, 19"temp_min": 281.15, 20"temp_max": 281.15 21}, 22"visibility": 10000, 23"wind": { 24"speed": 6.7, 25"deg": 300, 26"gust": 13.4 27}, 28"clouds": { 29"all": 20 30}, 31"dt": 1549000800, 32"sys": { 33"type": 1, 34"id": 8077, 35"message": 0.0064, 36"country": "JP", 37"sunrise": 1548970884, 38"sunset": 1549008478 39}, 40"id": 1850147, 41"name": "Tokyo", 42"cod": 200 43}

試したこと

https://qiita.com/hisayatanaka/items/6f0bb04f5e0d07775b87
上のURLを参考にして、返ってきたjsonをパースしてから以下のようにしてデータを取り出そうとしましたが、できませんでした。

go

1fmt.Println(weather.(map[string]interface{})["weather"].(map[string]interface{})["main"])

interface{}で取り出しているからか、index指定で出すこともできないようで、よくわかっていません。
Goは今日からやり始めました。

もしよろしければ改善方法を教えていただければ幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

map[base:stations main:map[temp:274.8 pressure:1015 humidity:38 temp_min:272.25 temp_max:277.15] visibility:10000 dt:1.5490224e+09 name:Tokyo cod:200 coord:map[lon:139.76 lat:35.68] weather:[map[main:Clouds description:few clouds icon:02n id:801]] wind:map[speed:7.7 deg:330] clouds:map[all:20] sys:map[country:JP sunrise:1.548970873e+09 sunset:1.549008493e+09 type:1 id:8074 message:0.0087] id:1.850147e+06]

上記のようなデータは、以下のように取得できるようです。
(ここではweather配下のmainを取得している)

fmt.Println(weather.(map[string]interface{})["weather"].([]interface{})[0].(map[string]interface{})["main"])

投稿2019/02/01 12:44

yutaro1204

総合スコア4

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問