2

I'm trying to access an object that it is part of an HTTP JSON Response, this one. What I want is to access only the location object from that response. If I access the full response from the developer console like this.

String url ='http://api.wunderground.com/api/48ba9f7756d42102/geolookup/conditions/forecast/q/Spain/Sevilla.json';

HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');

Http http = new Http();
HttpResponse res = http.send(req);
String responseBody = res.getBody();

system.debug('Weather response: ' + responseBody);

I get the full response exactly as the link above. That is fine. Now I want to access only the location JSON object by doing this:

  1. I created one class for this purpose:

    public with sharing class WeatherLocation {
    
    public location location{get;set;}
    
    public class Location {
        public String country_iso3166{get;set;}
        public String zip{get;set;}
        public String magic{get;set;}
        public String country{get;set;}
        public String wmo{get;set;}
        public String state_name{get;set;}
        public String latitude{get;set;}
        public String state{get;set;}
        public String longitude{get;set;}
        public String city{get;set;}
        public String elevation{get;set;}
        public String full{get;set;}
      }
    }
    
  2. I execute a new anonymous code:

    String url ='http://api.wunderground.com/api/48ba9f7756d42102/geolookup/conditions/forecast/q/Spain/Sevilla.json';
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');
    Http http = new Http();
    HttpResponse res = http.send(req);
    String responseBody = res.getBody();
    WeatherLocation.location Wloc = (WeatherLocation.location)JSON.deserialize(responseBody, WeatherLocation.location.class);
    system.debug('WeatherLocation  ' + Wloc);
    

The output that I'm getting:

20:21:59:367 USER_DEBUG [15]|DEBUG|Github user Location:[city=null, country=null, country_iso3166=null, elevation=null, full=null, latitude=null, longitude=null, magic=null, state=null, state_name=null, wmo=null, zip=null]

The response is sending back null values for each of the fields I want to use. Anyone knows what it is that I'm missing here. This approach works fine with other JSON responses, do I need to call some other JSON object in order to get access to the location object? Any help will be much appreciated.

payload:

  "location": {
    "type": "INTLCITY",
    "country": "SP",
    "country_iso3166": "ES",
    "country_name": "Spain",
    "state": "SE",
    "city": "Sevilla",
    "tz_short": "CEST",
    "tz_long": "Europe/Madrid",
    "lat": "37.38000107",
    "lon": "-5.98000002",
    "zip": "00000",
    "magic": "2",
    "wmo": "WLEEC",
    "l": "/q/zmw:00000.2.WLEEC",
    "requesturl": "global/stations/WLEEC.html",
    "wuiurl": "https://www.wunderground.com/global/stations/WLEEC.html",
    "nearby_weather_stations": {
      "airport": {
        "station": [
          {
            "city": "",
            "state": "",
            "country": "",
            "icao": "",
            "lat": "",
            "lon": ""
          },
          {
            "city": "Sevilla El Copero",
            "state": "",
            "country": "SP",
            "icao": "LEEC",
            "lat": "37.31000137",
            "lon": "-6.00000000"
          },
          {
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "icao": "LEZL",
            "lat": "37.41999817",
            "lon": "-5.90000010"
          },
          {
            "city": "Moron de la Frontera",
            "state": "",
            "country": "SP",
            "icao": "LEMO",
            "lat": "37.15000153",
            "lon": "-5.61999989"
          }
        ]
      },
      "pws": {
        "station": [
          {
            "neighborhood": "Sea cliff",
            "city": "Seville",
            "state": "",
            "country": "ES",
            "id": "ISEVILLE12",
            "lat": 37.382401,
            "lon": -5.9761,
            "distance_km": 0,
            "distance_mi": 0
          },
          {
            "neighborhood": "Calle General García de la Herranz",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA40",
            "lat": 37.374828,
            "lon": -5.975199,
            "distance_km": 0,
            "distance_mi": 0
          },
          {
            "neighborhood": "Luis de Montoto",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA36",
            "lat": 37.389091,
            "lon": -5.984459,
            "distance_km": 1,
            "distance_mi": 0
          },
          {
            "neighborhood": "Calle Guzmán el Bueno",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA61",
            "lat": 37.387226,
            "lon": -5.990362,
            "distance_km": 1,
            "distance_mi": 0
          },
          {
            "neighborhood": "Calle Velarde",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA31",
            "lat": 37.384609,
            "lon": -5.997752,
            "distance_km": 1,
            "distance_mi": 0
          },
          {
            "neighborhood": "Calle Sebastián Trujillo",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA47",
            "lat": 37.382309,
            "lon": -5.960624,
            "distance_km": 1,
            "distance_mi": 1
          },
          {
            "neighborhood": "Calle Virgen del Monte",
            "city": "Sevilla",
            "state": "",
            "country": "SP",
            "id": "ISEVILLA44",
            "lat": 37.377792,
            "lon": -6.000916,
            "distance_km": 1,
            "distance_mi": 1
          },
          {
            "neighborhood": "Calle Virgen de la Cinta",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA34",
            "lat": 37.372639,
            "lon": -5.99912,
            "distance_km": 1,
            "distance_mi": 1
          },
          {
            "neighborhood": "Avenida de la Reina Mercedes",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA71",
            "lat": 37.362957,
            "lon": -5.986244,
            "distance_km": 1,
            "distance_mi": 1
          },
          {
            "neighborhood": "Carretera de Carmona",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA67",
            "lat": 37.397945,
            "lon": -5.980408,
            "distance_km": 1,
            "distance_mi": 1
          },
          {
            "neighborhood": "Avenida Ramón de Carranza",
            "city": "Sevilla",
            "state": "",
            "country": "SP",
            "id": "ISEVILLA30",
            "lat": 37.371792,
            "lon": -6.002954,
            "distance_km": 2,
            "distance_mi": 1
          },
          {
            "neighborhood": "Calle Evangelista",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA75",
            "lat": 37.379345,
            "lon": -6.007031,
            "distance_km": 2,
            "distance_mi": 1
          },
          {
            "neighborhood": "Calle San Vicente",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA57",
            "lat": 37.396194,
            "lon": -5.999093,
            "distance_km": 2,
            "distance_mi": 1
          },
          {
            "neighborhood": "Sevilla - Triana",
            "city": "Sevilla",
            "state": "SEVILLA",
            "country": "ES",
            "id": "ISEVILLA13",
            "lat": 37.380402,
            "lon": -6.010914,
            "distance_km": 2,
            "distance_mi": 1
          },
          {
            "neighborhood": "Calle Aracena",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA77",
            "lat": 37.384449,
            "lon": -6.010558,
            "distance_km": 2,
            "distance_mi": 1
          },
          {
            "neighborhood": "Feria",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA51",
            "lat": 37.40292,
            "lon": -5.99197,
            "distance_km": 2,
            "distance_mi": 1
          },
          {
            "neighborhood": "Sevilla - Ronda de los Tejares",
            "city": "Sevilla",
            "state": "ANDALUCíA",
            "country": "ES",
            "id": "IANDALUC83",
            "lat": 37.380451,
            "lon": -6.013572,
            "distance_km": 2,
            "distance_mi": 1
          },
          {
            "neighborhood": "Calle Estrasburgo",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA29",
            "lat": 37.348221,
            "lon": -5.979222,
            "distance_km": 3,
            "distance_mi": 2
          },
          {
            "neighborhood": "Calle Cantina",
            "city": "Seville",
            "state": "",
            "country": "ES",
            "id": "ISEVILLE3",
            "lat": 37.419109,
            "lon": -5.98512,
            "distance_km": 4,
            "distance_mi": 2
          },
          {
            "neighborhood": "www.meteosanjuan.com",
            "city": "San Juan De Aznalfarache",
            "state": "SEVILLA",
            "country": "ES",
            "id": "ISEVILLA21",
            "lat": 37.360889,
            "lon": -6.030504,
            "distance_km": 4,
            "distance_mi": 2
          },
          {
            "neighborhood": "Calle Vicente Aleixandre",
            "city": "Tomares",
            "state": "",
            "country": "ES",
            "id": "ITOMARES5",
            "lat": 37.370872,
            "lon": -6.03558,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "Calle Profesor Tierno Galván",
            "city": "Tomares",
            "state": "",
            "country": "ES",
            "id": "ITOMARES7",
            "lat": 37.383041,
            "lon": -6.038856,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "Calle Ulises",
            "city": "Sevilla",
            "state": "",
            "country": "ES",
            "id": "ISEVILLA59",
            "lat": 37.396545,
            "lon": -5.923732,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "Sevilla - Hacienda San Antonio",
            "city": "Sevilla",
            "state": "Andalucía",
            "country": "ES",
            "id": "IALSEVIL3",
            "lat": 37.379246,
            "lon": -5.917678,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "San Juan de Aznalfarache",
            "city": "San Juan De Aznalfarache",
            "state": "SE",
            "country": "SP",
            "id": "ISESANJU3",
            "lat": 37.350018,
            "lon": -6.029826,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "Parque Alcosa",
            "city": "Sevilla",
            "state": "ANDALUC�A",
            "country": "ES",
            "id": "IANDALUC53",
            "lat": 37.407822,
            "lon": -5.926668,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "San Juan de Aznalfarache",
            "city": "San Juan De Aznalfarache",
            "state": "SE",
            "country": "SP",
            "id": "ISESANJU4",
            "lat": 37.356781,
            "lon": -6.037522,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "Calle Virgen de los Dolores",
            "city": "Tomares",
            "state": "",
            "country": "ES",
            "id": "ITOMARES2",
            "lat": 37.375854,
            "lon": -6.045006,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "Tomares - Aljamar",
            "city": "Tomares",
            "state": "",
            "country": "ES",
            "id": "ITOMARES8",
            "lat": 37.373432,
            "lon": -6.044942,
            "distance_km": 5,
            "distance_mi": 3
          },
          {
            "neighborhood": "La Cartuja",
            "city": "Tomares",
            "state": "ANDALUCíA",
            "country": "ES",
            "id": "IANDALUC181",
            "lat": 37.365551,
            "lon": -6.045215,
            "distance_km": 5,
            "distance_mi": 3
          }
        ]
      }
    }
  }
  • 1
    Shouldn't you just deserialize into a WheatherLocation instance, not WheatherLocation.Location? – Adrian Larson May 9 '17 at 18:55
  • Let me try that Adrian. – Carlos Naranjo May 9 '17 at 18:58
  • Adrian, that did it. Deserializing into WheaterLocation rather than WheaterLocation.Location is giving a response back for those fields. – Carlos Naranjo May 9 '17 at 19:02
2

You need to deserialize into the proper type. In this case, that's the top level class (WeatherLocation). I stripped out some irrelevant attributes from your payload and it came through clean. Here's the Execute Anonymous script I used to demonstrate:

public with sharing class WeatherLocation
{
    final Location location;
}
public class Location
{
    public final String country_iso3166, zip, magic, country;
    public final String wmo, state_name, latitude, state;
    public final String longitude, city, elevation, full;
}

String payload = '{"location":{"type":"INTLCITY","country":"SP","country_iso3166":"ES","country_name":"Spain","state":"SE","city":"Sevilla","tz_short":"CEST","tz_long":"Europe/Madrid","lat":"37.38000107","lon":"-5.98000002","zip":"00000","magic":"2","wmo":"WLEEC","l":"/q/zmw:00000.2.WLEEC","requesturl":"global/stations/WLEEC.html","wuiurl":"https://www.wunderground.com/global/stations/WLEEC.html"}}';
WeatherLocation instance = (WeatherLocation)JSON.deserialize(payload, WeatherLocation.class);
system.debug(instance);
  • @Carlos I didn't catch it myself until my anonymous script threw a compile fail! – Adrian Larson May 10 '17 at 20:51
  • 1
    That is one of the dangers of copy/paste :) – Carlos Naranjo May 10 '17 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.