ref : http://www.baku-dreameater.net/archives/8741 and revised. Article -> http://tech.guitarrapc.com/entry/2016/01/24/032718
void Main() | |
{ | |
//東京都のID | |
var cityCode = "130010"; | |
WeatherHacks.GetWeather(cityCode).Result.Dump(); | |
} | |
public class WeatherHacks | |
{ | |
private const string baseUrl = "http://weather.livedoor.com/forecast/webservice/json/v1"; | |
public static async Task<WeatherResponse[]> GetWeather(string cityCode) | |
{ | |
var url = $"{baseUrl}?city={cityCode}"; | |
var result = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseHeadersRead); | |
var jsonString = await result.Content.ReadAsStringAsync(); | |
var json = JObject.Parse(jsonString.ToString()); | |
var response = json["forecasts"].Select(x => new WeatherResponse {Date = DateTime.ParseExact(x["date"].ToString(), "yyyy-MM-dd", null), Telop = x["telop"].ToString()}).ToArray(); | |
return response; | |
} | |
} | |
public class WeatherResponse | |
{ | |
public DateTime Date { get; set; } | |
public string Telop { get; set; } | |
} |
void Main() | |
{ | |
//東京都のID | |
var cityCode = "130010"; | |
WeatherHacksClass.GetWeather(cityCode).Result.Dump(); | |
} | |
public class WeatherHacksClass | |
{ | |
private const string baseUrl = "http://weather.livedoor.com/forecast/webservice/json/v1"; | |
public static async Task<WeatherResponse> GetWeather(string cityCode) | |
{ | |
var url = $"{baseUrl}?city={cityCode}"; | |
var result = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseHeadersRead); | |
var jsonString = await result.Content.ReadAsStringAsync(); | |
var response = JsonConvert.DeserializeObject<WeatherResponse>(jsonString); | |
return response; | |
} | |
} | |
public class WeatherResponse | |
{ | |
public Pinpointlocation[] pinpointLocations { get; set; } | |
public string link { get; set; } | |
public Forecast[] forecasts { get; set; } | |
public Location location { get; set; } | |
public DateTime publicTime { get; set; } | |
public Copyright copyright { get; set; } | |
public string title { get; set; } | |
public Description description { get; set; } | |
} | |
public class Location | |
{ | |
public string city { get; set; } | |
public string area { get; set; } | |
public string prefecture { get; set; } | |
} | |
public class Copyright | |
{ | |
public Provider[] provider { get; set; } | |
public string link { get; set; } | |
public string title { get; set; } | |
public Image image { get; set; } | |
} | |
public class Image | |
{ | |
public int width { get; set; } | |
public string link { get; set; } | |
public string url { get; set; } | |
public string title { get; set; } | |
public int height { get; set; } | |
} | |
public class Provider | |
{ | |
public string link { get; set; } | |
public string name { get; set; } | |
} | |
public class Description | |
{ | |
public string text { get; set; } | |
public DateTime publicTime { get; set; } | |
} | |
public class Pinpointlocation | |
{ | |
public string link { get; set; } | |
public string name { get; set; } | |
} | |
public class Forecast | |
{ | |
public string dateLabel { get; set; } | |
public string telop { get; set; } | |
public string date { get; set; } | |
public Temperature temperature { get; set; } | |
public Image1 image { get; set; } | |
} | |
public class Temperature | |
{ | |
public Min min { get; set; } | |
public Max max { get; set; } | |
} | |
public class Min | |
{ | |
public string celsius { get; set; } | |
public string fahrenheit { get; set; } | |
} | |
public class Max | |
{ | |
public string celsius { get; set; } | |
public string fahrenheit { get; set; } | |
} | |
public class Image1 | |
{ | |
public int width { get; set; } | |
public string url { get; set; } | |
public string title { get; set; } | |
public int height { get; set; } | |
} | |
function Main | |
{ | |
$cityCode = "130010"; | |
[WeatherHacks]::GetWeather($cityCode); | |
} | |
class WeatherHacks | |
{ | |
static [string]$baseUrl = "http://weather.livedoor.com/forecast/webservice/json/v1"; | |
static [WeatherResponse] GetWeather([string]$cityCode) | |
{ | |
$url = "$([WeatherHacks]::baseUrl)?city=$cityCode"; | |
[WeatherResponse]$result = Invoke-RestMethod -Method Get -Uri $url -UseBasicParsing; | |
return $result; | |
} | |
} | |
class WeatherResponse | |
{ | |
[Pinpointlocation[]]$pinpointLocations; | |
[string]$link; | |
[Forecast[]]$forecasts; | |
[Location]$location; | |
[DateTime]$publicTime; | |
[Copyright]$copyright; | |
[string]$title; | |
[Description]$description; | |
} | |
class Location | |
{ | |
[string]$city; | |
[string]$area; | |
[string]$prefecture; | |
} | |
class Copyright | |
{ | |
[Provider[]]$provider; | |
[string]$link; | |
[string]$title; | |
[Image]$image; | |
} | |
class Image | |
{ | |
[int]$width; | |
[string]$link; | |
[string]$url; | |
[string]$title; | |
[int]$height; | |
} | |
class Provider | |
{ | |
[string]$link; | |
[string]$name; | |
} | |
class Description | |
{ | |
[string]$text; | |
[DateTime]$publicTime; | |
} | |
class Pinpointlocation | |
{ | |
[string]$link; | |
[string]$name; | |
} | |
class Forecast | |
{ | |
[string]$dateLabel; | |
[string]$telop; | |
[string]$date; | |
[Temperature]$temperature; | |
[Image1]$image; | |
} | |
class Temperature | |
{ | |
[Min]$min; | |
[Max]$max; | |
} | |
class Min | |
{ | |
[string]$celsius; | |
[string]$fahrenheit; | |
} | |
class Max | |
{ | |
[string]$celsius; | |
[string]$fahrenheit; | |
} | |
class Image1 | |
{ | |
[int]$width; | |
[string]$url; | |
[string]$title; | |
[int]$height; | |
} | |
Main |
function Main | |
{ | |
$cityCode = "130010"; | |
Get-WeatherHacksWeather -CityCode $cityCode | |
} | |
function Get-WeatherHacksWeather | |
{ | |
[OutputType([PSCustomObject])] | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Mandatory = $true)] | |
[string]$cityCode | |
) | |
$private:baseUrl = "http://weather.livedoor.com/forecast/webservice/json/v1"; | |
$url = "$($baseUrl)?city=$cityCode"; | |
$result = Invoke-RestMethod -Method Get -Uri $url -UseBasicParsing; | |
return $result; | |
} | |
Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
C# Class
PowerShell Class
PowerShell Function