【问题标题】:Reading content from REST web service in MATLAB在 MATLAB 中从 REST Web 服务读取内容
【发布时间】:2017-12-03 17:22:37
【问题描述】:

我正在使用 webread() 函数从 sunrise-sunset.org api here 检索日出和日落数据。

这是我的代码的样子:

function [E_total] = solar_energy(lng, lat, yr, month, day)

% Generate URL
url = strcat('https://api.sunrise-sunset.org/json?lat=', num2str(lat),...
       '&lng=', num2str(lng), '&date=', num2str(yr), '-', num2str(month),... 
       '-',num2str(day));

% Retrieve data
forecast = webread(url)

if isempty(forecast)  % Failed, use default estimates
    sunrise = 6;
    sunset = 18;
    noon = 12;
elseif forecast.status == 'OK'
    % Success!  Parse retrieved data...
    forecast.results
    dv = datevec(forecast.results.sunrise)
    sunrise = dv(6)/3600 + dv(5)/60 + dv(4)
    dv = datevec(forecast.results.sunset)
    sunset = dv(6)/3600 + dv(5)/60 + dv(4)
    dv = datevec(forecast.results.solar_noon)
    noon = dv(6)/3600 + dv(5)/60 + dv(4)
end

这是我在华盛顿国家纪念碑获得的 2017 年 12 月 2 日日出和日落数据的示例。

输入:

solar_energy(-77.0353, 38.8895, 2017, 12, 02)

这是我得到的:

预测 =

results: [1x1 struct]
 status: 'OK'

ans =

                    sunrise: '12:00:01 AM'
                     sunset: '12:00:01 AM'
                 solar_noon: '9:38:38 AM'
                 day_length: '00:00:00'
       civil_twilight_begin: '12:00:01 AM'
         civil_twilight_end: '12:00:01 AM'
    nautical_twilight_begin: '12:00:01 AM'
      nautical_twilight_end: '12:00:01 AM'
astronomical_twilight_begin: '12:00:01 AM'
  astronomical_twilight_end: '12:00:01 AM'

我的方法有问题还是这个 api 有问题? 数据已成功检索,但对于所有日期,日出和日落时间要么显示为上午 12:00:00,要么为上午 9 点。

【问题讨论】:

  • 嗯...如果从 API 调用返回的数据错误,则更改是 API 失败。我尝试运行您的代码,但遇到了同样的问题,但使用“PM”。

标签: json matlab rest web-services


【解决方案1】:

好的,我想我找到了你的问题。我看了一下API documentation,发现它支持一个名为formatted的附加参数,描述如下:

格式化(整数):0 或 1(默认为 1)。响应时间值 将按照 ISO 8601 表示,day_length 将表示 马上。可选。

我尝试将其附加到您在函数中创建的请求中。同时,我还修复了您在通话中使用的date parameter 的一个小问题:

date(字符串):YYYY-MM-DD 格式的日期。也接受其他日期 格式,甚至是相对日期格式。如果不存在,则默认日期 到当前日期。可选。

天的“DD”格式以两位数表示天值(例如:21,如果天值是 2102,如果天值是 2)。由于num2str(2) = '2'num2str(02) = '2',使用num2str 不会重现此行为。一个快速的解决办法是改用datestr(day,'dd')

这是最终结果:

solar_energy(-77.0353, 38.8895, 2017, 12, 02);

function [E_total] = solar_energy(lng, lat, yr, month, day)

    url = strcat('https://api.sunrise-sunset.org/json', ...
        '?lat=', num2str(lat), ...
        '&lng=', num2str(lng), ...
        '&date=', num2str(yr),'-',num2str(month),'-',datestr(day,'dd'), ...
        '&formatted=0');

    forecast = webread(url);

    if (isempty(forecast) || ~strcmp(forecast.status,'OK'))
        sunrise = 6;
        noon = 12;
        sunset = 18;
    else
        forecast.results
    end

end

这是上面代码产生的结果:

                    sunrise: '2017-12-02T12:09:28+00:00'
                     sunset: '2017-12-02T21:46:20+00:00'
                 solar_noon: '2017-12-02T16:57:54+00:00'
                 day_length: 34612
       civil_twilight_begin: '2017-12-02T11:40:03+00:00'
         civil_twilight_end: '2017-12-02T22:15:44+00:00'
    nautical_twilight_begin: '2017-12-02T11:06:58+00:00'
      nautical_twilight_end: '2017-12-02T22:48:50+00:00'
astronomical_twilight_begin: '2017-12-02T10:34:47+00:00'
  astronomical_twilight_end: '2017-12-02T23:21:01+00:00'

如您所见,返回的值看起来是正确的。所以问题是由API如何处理它从数据库中检索到的ISO 8601中的日期转换为另一种格式引起的。

当然,您必须更改当前解析API 返回值的方式。这应该可以完成工作:

datevec(struct.sunrise,'yyyy-mm-ddTHH:MM:ss');

【讨论】:

  • 完美!!谢谢楼主!!
猜你喜欢
  • 2021-05-27
  • 2016-11-22
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
相关资源
最近更新 更多