I converted Mapquest geocoded data into an Apex Wrapper using JSON2APEX. Now I need to access the location information longitude and latitude:
Mapquest JSON:
{
"info": {
"statuscode": 0,
"copyright": {
"text": "© 2018 MapQuest, Inc.",
"imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
"imageAltText": "© 2018 MapQuest, Inc."
},
"messages": []
},
"options": {
"maxResults": -1,
"thumbMaps": true,
"ignoreLatLngInput": false
},
"results": [
{
"providedLocation": {
"location": "Washington,DC"
},
"locations": [
{
"street": "",
"adminArea6": "",
"adminArea6Type": "Neighborhood",
"adminArea5": "Washington",
"adminArea5Type": "City",
"adminArea4": "District of Columbia",
"adminArea4Type": "County",
"adminArea3": "DC",
"adminArea3Type": "State",
"adminArea1": "US",
"adminArea1Type": "Country",
"postalCode": "",
"geocodeQualityCode": "A5XAX",
"geocodeQuality": "CITY",
"dragPoint": false,
"sideOfStreet": "N",
"linkId": "282772166",
"unknownInput": "",
"type": "s",
"latLng": {
"lat": 38.892062,
"lng": -77.019912
},
"displayLatLng": {
"lat": 38.892062,
"lng": -77.019912
},
"mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=KEY&type=map&size=225,160&pois=purple-1,38.892062,-77.019912,0,0,|¢er=38.892062,-77.019912&zoom=12&rand=306744981"
}
]
}
]
}
JSON2APEX
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
// The supplied json has fields with names that are not valid in apex
// and so can only be parsed with explicitly generated code, this option
// was auto selected for you.
public class ClosestStore_Wrapper {
public class Copyright {
public String text {get;set;}
public String imageUrl {get;set;}
public String imageAltText {get;set;}
public Copyright(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'text') {
text = parser.getText();
} else if (text == 'imageUrl') {
imageUrl = parser.getText();
} else if (text == 'imageAltText') {
imageAltText = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Copyright consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Options {
public Integer maxResults {get;set;}
public Boolean thumbMaps {get;set;}
public Boolean ignoreLatLngInput {get;set;}
public Options(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'maxResults') {
maxResults = parser.getIntegerValue();
} else if (text == 'thumbMaps') {
thumbMaps = parser.getBooleanValue();
} else if (text == 'ignoreLatLngInput') {
ignoreLatLngInput = parser.getBooleanValue();
} else {
System.debug(LoggingLevel.WARN, 'Options consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Messages {
public Messages(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
{
System.debug(LoggingLevel.WARN, 'Messages consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class ProvidedLocation {
public String location {get;set;}
public ProvidedLocation(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'location') {
location = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'ProvidedLocation consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Results {
public ProvidedLocation providedLocation {get;set;}
public List<Locations> locations {get;set;}
public Results(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'providedLocation') {
providedLocation = new ProvidedLocation(parser);
} else if (text == 'locations') {
locations = arrayOfLocations(parser);
} else {
System.debug(LoggingLevel.WARN, 'Results consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Locations {
public String street {get;set;}
public String adminArea6 {get;set;}
public String adminArea6Type {get;set;}
public String adminArea5 {get;set;}
public String adminArea5Type {get;set;}
public String adminArea4 {get;set;}
public String adminArea4Type {get;set;}
public String adminArea3 {get;set;}
public String adminArea3Type {get;set;}
public String adminArea1 {get;set;}
public String adminArea1Type {get;set;}
public String postalCode {get;set;}
public String geocodeQualityCode {get;set;}
public String geocodeQuality {get;set;}
public Boolean dragPoint {get;set;}
public String sideOfStreet {get;set;}
public String linkId {get;set;}
public String unknownInput {get;set;}
public String type_Z {get;set;} // in json: type
public LatLng latLng {get;set;}
public LatLng displayLatLng {get;set;}
public String mapUrl {get;set;}
public Locations(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'street') {
street = parser.getText();
} else if (text == 'adminArea6') {
adminArea6 = parser.getText();
} else if (text == 'adminArea6Type') {
adminArea6Type = parser.getText();
} else if (text == 'adminArea5') {
adminArea5 = parser.getText();
} else if (text == 'adminArea5Type') {
adminArea5Type = parser.getText();
} else if (text == 'adminArea4') {
adminArea4 = parser.getText();
} else if (text == 'adminArea4Type') {
adminArea4Type = parser.getText();
} else if (text == 'adminArea3') {
adminArea3 = parser.getText();
} else if (text == 'adminArea3Type') {
adminArea3Type = parser.getText();
} else if (text == 'adminArea1') {
adminArea1 = parser.getText();
} else if (text == 'adminArea1Type') {
adminArea1Type = parser.getText();
} else if (text == 'postalCode') {
postalCode = parser.getText();
} else if (text == 'geocodeQualityCode') {
geocodeQualityCode = parser.getText();
} else if (text == 'geocodeQuality') {
geocodeQuality = parser.getText();
} else if (text == 'dragPoint') {
dragPoint = parser.getBooleanValue();
} else if (text == 'sideOfStreet') {
sideOfStreet = parser.getText();
} else if (text == 'linkId') {
linkId = parser.getText();
} else if (text == 'unknownInput') {
unknownInput = parser.getText();
} else if (text == 'type') {
type_Z = parser.getText();
} else if (text == 'latLng') {
latLng = new LatLng(parser);
} else if (text == 'displayLatLng') {
displayLatLng = new LatLng(parser);
} else if (text == 'mapUrl') {
mapUrl = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Locations consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class LatLng {
public Double lat {get;set;}
public Double lng {get;set;}
public LatLng(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'lat') {
lat = parser.getDoubleValue();
} else if (text == 'lng') {
lng = parser.getDoubleValue();
} else {
System.debug(LoggingLevel.WARN, 'LatLng consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public class Info {
public Integer statuscode {get;set;}
public Copyright copyright {get;set;}
public List<Messages> messages {get;set;}
public Info(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'statuscode') {
statuscode = parser.getIntegerValue();
} else if (text == 'copyright') {
copyright = new Copyright(parser);
} else if (text == 'messages') {
messages = arrayOfMessages(parser);
} else {
System.debug(LoggingLevel.WARN, 'Info consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
public Info info {get;set;}
public Options options {get;set;}
public List<Results> results {get;set;}
public ClosestStore_Wrapper(JSONParser parser) {
while (parser.nextToken() != System.JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
if (text == 'info') {
info = new Info(parser);
} else if (text == 'options') {
options = new Options(parser);
} else if (text == 'results') {
results = arrayOfResults(parser);
} else {
System.debug(LoggingLevel.WARN, 'ClosestStore_Wrapper consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
public static ClosestStore_Wrapper parse(String json) {
System.JSONParser parser = System.JSON.createParser(json);
return new ClosestStore_Wrapper(parser);
}
public static void consumeObject(System.JSONParser parser) {
Integer depth = 0;
do {
System.JSONToken curr = parser.getCurrentToken();
if (curr == System.JSONToken.START_OBJECT ||
curr == System.JSONToken.START_ARRAY) {
depth++;
} else if (curr == System.JSONToken.END_OBJECT ||
curr == System.JSONToken.END_ARRAY) {
depth--;
}
} while (depth > 0 && parser.nextToken() != null);
}
private static List<Locations> arrayOfLocations(System.JSONParser p) {
List<Locations> res = new List<Locations>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Locations(p));
}
return res;
}
private static List<Results> arrayOfResults(System.JSONParser p) {
List<Results> res = new List<Results>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Results(p));
}
return res;
}
private static List<Messages> arrayOfMessages(System.JSONParser p) {
List<Messages> res = new List<Messages>();
if (p.getCurrentToken() == null) p.nextToken();
while (p.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Messages(p));
}
return res;
}
}
HTTP response:
if(res.getStatusCode() == 200) {
ClosestStore_Wrapper wrapperinstance;
wrapperinstance = (ClosestStore_Wrapper)JSON.deserialize(res.getBody(), ClosestStore_Wrapper.class);
System.debug(logginglevel.debug, 'wrapperinstance '+wrapperinstance.Results);
System.debug(logginglevel.debug, 'acc '+acc);
//acc.Location__latitude__s = wrapperinstance.Results.locations[0].LatLng.lat;
//acc.Location__longitude__s = wrapperinstance.Results.locations[0].LatLng.lng;
//update acc;
}
How do I need to modify the APEX2JSON class in order to be able to access lat and lng? Thanks!