AppleScript名:指定URLのJSONをダウンロードしてRecordに変換 v2.1 |
— Created 2016-02-06 by Takaaki Naganoya — Modified 2016-02-08 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" –東京電力電力供給状況API JSON URL(指定された日時の電力使用状況を返す) set a to downloadAndParseJSON("http://tepco-usage-api.appspot.com/2016/2/6/19.json") of me –> {forecast_peak_period:18, forecast_peak_usage:3670, capacity_updated:"2012-02-05 08:30:00", usage:3720, forecast_peak_updated:"2012-02-05 08:30:00", day:6, usage_updated:"2016-02-06 11:05:05", capacity:4484, saving:false, year:2016, month:2, capacity_peak_period:18, forecast:0, hour:19, entryfor:"2016-02-06 10:00:00"} –東京電力電力供給状況API JSON URL(指定された日の毎時の電力使用状況を、配列として返す) set b to downloadAndParseJSON("http://tepco-usage-api.appspot.com/2016/2/6.json") of me –> {{forecast_peak_period:18, forecast_peak_usage:3670, capacity_updated:"2012-02-05 08:30:00", usage:3081, forecast_peak_updated:"2012-02-05 08:30:00", day:6, usage_updated:"2016-02-05 16:05:06", capacity:4484, saving:false, year:2016, month:2, capacity_peak_period:18, forecast:0, hour:0, entryfor:"2016-02-05 15:00:00"}….…{forecast_peak_period:18, forecast_peak_usage:3670, capacity_updated:"2012-02-05 08:30:00", usage:3645, forecast_peak_updated:"2012-02-05 08:30:00", day:6, usage_updated:"2016-02-06 12:05:07", capacity:4484, saving:false, year:2016, month:2, capacity_peak_period:18, forecast:0, hour:20, entryfor:"2016-02-06 11:00:00"}} –東京電力電力供給状況API JSON URL(指定された月の毎日毎時の電力使用状況を、配列として返す) set c to downloadAndParseJSON("http://tepco-usage-api.appspot.com/2016/2.json") of me –> {{forecast_peak_period:18, forecast_peak_usage:4260, capacity_updated:"2012-01-31 23:30:00", usage:2901, forecast_peak_updated:"2012-01-31 23:30:00", day:1, usage_updated:"2016-01-31 16:05:04", capacity:4641, saving:false, year:2016, month:2, capacity_peak_period:18, forecast:0, hour:0, entryfor:"2016-01-31 15:00:00"}……{forecast_peak_period:18, forecast_peak_usage:3670, capacity_updated:"2012-02-05 08:30:00", usage:3645, forecast_peak_updated:"2012-02-05 08:30:00", day:6, usage_updated:"2016-02-06 12:05:07", capacity:4484, saving:false, year:2016, month:2, capacity_peak_period:18, forecast:0, hour:20, entryfor:"2016-02-06 11:00:00"}} on downloadAndParseJSON(aURL) set aRes to downloadDataFromWeb(aURL, 30) of me if aRes = false then return false –download error set jsonString to current application’s NSString’s stringWithString:aRes set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding) set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value) set bRes to aJsonDict as list of string or string return bRes end downloadAndParseJSON on downloadDataFromWeb(aURL, timeOutSec) set aURL to current application’s |NSURL|’s alloc()’s initWithString:aURL set aReq to current application’s NSURLRequest’s alloc()’s initWithURL:aURL cachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:timeOutSec set urlRes to (current application’s NSURLConnection’s sendSynchronousRequest:aReq returningResponse:(missing value) |error|:(missing value)) if urlRes = missing value then return false –Download Error else set aVal to (current application’s NSString’s alloc()’s initWithData:urlRes encoding:(current application’s NSUTF8StringEncoding)) return aVal end if end downloadDataFromWeb |
More from my site
(Visited 27 times, 1 visits today)