2017-07-04 378 views
2

我是Go的新手,我正在使用天氣API,我已經註釋了導致錯誤的部分,我已經看到其他幾個鏈接有類似的問題,但是他們似乎沒有在JSON字符串中間的數組。我確信有一種方法來定義結構與切片。我似乎無法得到的語法允許它這裏是我堅持:。unmarshal json array into go結構體(數組位於JSON字符串的中間)

package main 

import (
    "encoding/json" 
    "fmt" 
    "io/ioutil" 
    "log" 
    "net/http" 
) 

// WeatherData struct to collect data from the API call 
type WeatherData struct { 
    Wind Wind 
    Sys Sys 
    // Weather Weather 
    Name string `json:"name"` 
} 

////////////// ERROR when unmarshalling this struct ///////// 
// Weather provides basic weather info 
// type Weather struct { 
// ID  int `json:"id"` 
// Descrip string `json:"description"` 
// Icon string `json:"icon"` 
// } 
///////////////////////////////////////////////////////////// 

// Sys includes sunrise, sunset, country, etc. 
type Sys struct { 
    Country string `json:"country"` 
} 

// Wind struct to get specific wind characteristics 
type Wind struct { 
    Speed float64 `json:"speed"` 
    Degree float64 `json:"deg"` 
    Gust float64 `json:"gust"` 
} 

func main() { 
    res, getErr := http.Get("http://api.openweathermap.org/data/2.5/weather?zip=REMOVED,us&appid=REMOVEDBUTWILLPOSTJSONData") 
    if getErr != nil { 
     log.Fatalln("http.Get error: ", getErr) 
    } 
    defer res.Body.Close() 
    body, readErr := ioutil.ReadAll(res.Body) 
    if readErr != nil { 
     log.Fatalln("Read Error: ", readErr) 
    } 
//////////// UNABLE TO UNMARSHAL the array that passes through here //// 
    var data WeatherData 
    if err := json.Unmarshal(body, &data); err != nil { 
     panic(err) 
    } 

    fmt.Println("Wind gusts: ", data.Wind.Gust) 
    fmt.Println("Wind speed: ", data.Wind.Speed) 
    fmt.Println("Wind degrees: ", data.Wind.Degree) 

    fmt.Println("Country is: ", data.Sys.Country) 
    fmt.Println("City is: ", data.Name) 

///////////////// CAN'T ACCESS Description...or anything in Weather 
// fmt.Println("Country is: ", data.Weather.Descrip) // cannot access due to this portion being inside an array 

} 



/////////////////THIS IS THE JSON DATA THAT IS AVAILABLE /////////// 
{ 
    "coord": { 
    "lon": -97.31, 
    "lat": 32.94 
    }, 
    "weather": [ // CAN'T ACCESS THIS CORRECTLY 
    { 
     "id": 800, 
     "main": "Clear", 
     "description": "clear sky", 
     "icon": "01d" 
    } 
    ], 
    "base": "stations", 
    "main": { 
    "temp": 306.46, 
    "pressure": 1014, 
    "humidity": 55, 
    "temp_min": 306.15, 
    "temp_max": 307.15 
    }, 
    "visibility": 16093, 
    "wind": { 
    "speed": 5.1, 
    "deg": 150, 
    "gust": 7.2 
    }, 
    "clouds": { 
    "all": 1 
    }, 
    "dt": 1499120100, 
    "sys": { 
    "type": 1, 
    "id": 2597, 
    "message": 0.0225, 
    "country": "US", 
    "sunrise": 1499081152, 
    "sunset": 1499132486 
    }, 
    "id": 0, 
    "name": "Fort Worth", 
    "cod": 200 
} 
+0

其他鏈接我已經看到有一個非常類似的問題,但解決方案不太適合我的情況:https://stackoverflow.com/questions/ 40626125/golang-unmarshal-json-can-unmarshal-array-into-go-value-type-main-moni –

+0

此鏈接看起來最接近,但我不知道如何根據他們的解決方案創建自定義unmarshaller描述... https://stackoverflow.com/questions/42377989/unmarshal-json-array-of-arrays-in-go –

+0

更新:它仍然不工作,但我已經改變:'類型WeatherData結構{ \t Wind Wind \t Sys Sys \t Weather []天氣//////////這會導致數值出現,但我似乎失去了根據鍵值識別數值的能力 \t Name string 'json:「name」' }' –

回答

3

你必須WeatherData定義Weather結構的片

取消註釋Weather結構和更新WeatherData結構以下。

// WeatherData struct to collect data from the API call 
type WeatherData struct { 
    Wind Wind  `json:"wind"` 
    Sys  Sys  `json:"sys"` 
    Weather []Weather `json:"weather"` 
    Name string `json:"name"` 
} 

請對示例代碼來看看:https://play.golang.org/p/4KFqRuxcx2

+0

謝謝你試圖提供幫助,但我已經能夠做到這一點,並獲得大量的數組數據,但我仍然無法調用某個特定的鍵/值.Weather.Descrip會返回未定義的錯誤。我也嘗試添加類型[] map [string] interface {},它可以爲我提供所有的鍵/值對,但我仍然無法調用它們。你可以在這裏看到我的改變:[鏈接](https://play.golang.org/p/MaZ5wMuJVA) –

+0

你需要通過索引訪問切片/數組值,參見這裏:[link](https:// play。 golang.org/p/zks-JPQ1YY)。 – jeevatkm

+0

它的工作原理!非常感謝。我甚至可以通過索引0訪問整個事物,但是沒有意識到我可以在其上做點符號。再次感謝! –