Skip to content

Instantly share code, notes, and snippets.

@andynu
Created November 30, 2015 20:05
Show Gist options
  • Select an option

  • Save andynu/a983d0db1351ac84c1e5 to your computer and use it in GitHub Desktop.

Select an option

Save andynu/a983d0db1351ac84c1e5 to your computer and use it in GitHub Desktop.
Parsing JSON Issue
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"hourly": 1
}
}
,
"hourly_forecast": [
{
"FCTTIME": {
"hour": "15","hour_padded": "15","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "11","mon_padded": "11","mon_abbrev": "Nov","mday": "30","mday_padded": "30","yday": "333","isdst": "0","epoch": "1448913600","pretty": "3:00 PM EST on November 30, 2015","civil": "3:00 PM","month_name": "November","month_name_abbrev": "Nov","weekday_name": "Monday","weekday_name_night": "Monday Night","weekday_name_abbrev": "Mon","weekday_name_unlang": "Monday","weekday_name_night_unlang": "Monday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "37", "metric": "3"},
"dewpoint": {"english": "25", "metric": "-4"},
"condition": "Mostly Cloudy",
"icon": "mostlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/mostlycloudy.gif",
"fctcode": "3",
"sky": "69",
"wspd": {"english": "7", "metric": "11"},
"wdir": {"dir": "SE", "degrees": "138"},
"wx": "Mostly Cloudy",
"uvi": "0",
"humidity": "62",
"windchill": {"english": "32", "metric": "0"},
"heatindex": {"english": "-9999", "metric": "-9999"},
"feelslike": {"english": "32", "metric": "0"},
"qpf": {"english": "0.0", "metric": "0"},
"snow": {"english": "0.0", "metric": "0"},
"pop": "0",
"mslp": {"english": "30.44", "metric": "1031"}
}
,
{
"FCTTIME": {
"hour": "16","hour_padded": "16","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "11","mon_padded": "11","mon_abbrev": "Nov","mday": "30","mday_padded": "30","yday": "333","isdst": "0","epoch": "1448917200","pretty": "4:00 PM EST on November 30, 2015","civil": "4:00 PM","month_name": "November","month_name_abbrev": "Nov","weekday_name": "Monday","weekday_name_night": "Monday Night","weekday_name_abbrev": "Mon","weekday_name_unlang": "Monday","weekday_name_night_unlang": "Monday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "36", "metric": "2"},
"dewpoint": {"english": "24", "metric": "-4"},
"condition": "Mostly Cloudy",
"icon": "mostlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/mostlycloudy.gif",
"fctcode": "3",
"sky": "72",
"wspd": {"english": "6", "metric": "10"},
"wdir": {"dir": "SE", "degrees": "146"},
"wx": "Mostly Cloudy",
"uvi": "0",
"humidity": "60",
"windchill": {"english": "31", "metric": "-1"},
"heatindex": {"english": "-9999", "metric": "-9999"},
"feelslike": {"english": "31", "metric": "-1"},
"qpf": {"english": "0.0", "metric": "0"},
"snow": {"english": "0.0", "metric": "0"},
"pop": "0",
"mslp": {"english": "30.42", "metric": "1030"}
}
]
}
{{0.1 http://www.wunderground.com/weather/api/d/terms.html {%!s(int=0)}} []}%
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type WUResponseInfo struct {
Version string
TermsOfService string `json: "termsofService"`
Features struct {
Hourly int
}
}
type WUEnglishMetric struct {
English string
Metric string
}
type WUHourlyInfo struct {
FCTime struct {
Hour string
HourPadded string
Min string
MinUnpadded string
Sec string
Year string
Mon string
Mon_padded string
Mon_abbrev string
Mday string
MdayPadded string
Yday string
Isdst string
Epoch string
Pretty string
Civil string
MonthName string
MonthNameAbbrev string
WeekdayName string
WeekdayNameNight string
WeekdayNameAbbrev string
WeekdayNameUnlang string
WeekdayNameNightUnlang string
Ampm string
Tz string
Age string
UTCDATE string
}
Temp WUEnglishMetric
Dewpoint WUEnglishMetric
Condition string
Icon string
IconUrl string `json: "icon_url"`
Fctcode string
Sky string
Wspd WUEnglishMetric
Wdir struct {
Dir string
Degrees string
}
Wx string
Uvi string
Humidity string
Windchill WUEnglishMetric
Heatindex WUEnglishMetric
Feelslike WUEnglishMetric
Qpf WUEnglishMetric
Snow WUEnglishMetric
Pop string
Mslp WUEnglishMetric
}
type WUHourlyResponse struct {
Response WUResponseInfo
HourlyForecast []map[string]interface{} `json: "hourly_forecast"`
}
func main() {
config, err := loadJsonConfig()
if err != nil {
panic(err)
}
weatherUndergroundKey := config["weather_underground_key"]
location := config["weather_underground_location"] // e.g. // "CA/San_Francisco"
hourlyWeatherUrl := "http://api.wunderground.com/api/" + weatherUndergroundKey + "/hourly/q/" + location + ".json"
fmt.Println(hourlyWeatherUrl)
//res, err := http.Get(hourlyWeatherUrl)
//if err != nil {
// panic(err)
//}
//defer res.Body.Close()
//body, err := ioutil.ReadAll(res.Body)
body, err := ioutil.ReadFile("/home/andy/rfk/weather.hourly.response.json")
if err != nil {
panic(err)
}
var hourlyWeather WUHourlyResponse
err = json.Unmarshal(body, &hourlyWeather)
if err != nil {
panic(err)
}
fmt.Printf("%s", body)
fmt.Println("---")
fmt.Printf("%s", hourlyWeather)
//fmt.Printf("Results: %v\n", weatherUndergroundKey)
}
// TODO: This should be in the data dir, or generic home ...
func jsonConfigFile() string {
return "/home/andy/rfk/weather.json"
}
// loads a single level hash config. e.g. { "a": "first", "b": "second" }
func loadJsonConfig() (map[string]string, error) {
type jsonConfig map[string]string
var config jsonConfig
file, err := ioutil.ReadFile(jsonConfigFile())
if err != nil {
return config, err
}
err = json.Unmarshal(file, &config)
if err != nil {
return config, err
}
return config, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment