Skip to main content

public with sharing class WeatherWrapper {

@AuraEnabled

public Forecast forecast;

public class Forecast{

@AuraEnabled

public List<Forecastday> forecastday;

}

public class Forecastday {

@AuraEnabled

public List<Hour> hour;

}

public class Hour {

@AuraEnabled

@SerializedName('time')

public String time_Z;

@AuraEnabled

public Condition condition;

@AuraEnabled

public Integer humidity;

@AuraEnabled

public Double temp_c;

@AuraEnabled

public Double wind_mph;

}

public class Condition {

@AuraEnabled

public String text;

@AuraEnabled

public String icon;

}

public static WeatherWrapper parse(String json) {

return (WeatherWrapper) System.JSON.deserialize(json, WeatherWrapper.class);

}

}

public static WeatherWrapper forecastWeather1(String city){

List<WeatherWrapper.Forecast> forecast = new List<WeatherWrapper.Forecast>();

Http http = new Http();

HttpRequest req = new HttpRequest();

req.setEndpoint('callout:WeatherAPI/forecast.json?q=' + city);

req.setMethod('GET');

HttpResponse res = http.send(req);

WeatherWrapper weatherResponse = WeatherWrapper.parse(res.getBody());

return weatherResponse;

}

 
1 respuesta
  1. 22 ago 2023, 10:04 a.m.

    Hi Roman,

    After getting the JSON response after the below line:

    HttpRequest req = new HttpRequest();

    Store the response in a variable and replace the 'time' keyword with 'time_Z' in JSON response:

    String responseBody = res.getBody().replaceAll('time', 'time_Z');

    Then parse the JSON like this:

    WeatherWrapper weatherResponse = WeatherWrapper.parse(responseBody);

    If this information helps, please mark the answer as best.

    Thank you