function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aviator517Aviator517 

Access values in JSON Parser class

I have this JSON parser class:

public class GoogleAPIParser {

    public class Address_components {
        public String long_name;
        public String short_name;
        public List<String> types;
    }

    public List<Results> results;
    public String status;

    public class Location {
        public Double lat;
        public Double lng;
    }

    public class Geometry {
        public Location location;
        public String location_type;
        public Viewport viewport;
    }

    public class Results {
        public List<Address_components> address_components;
        public String formatted_address;
        public Geometry geometry;
        public List<String> types;
    }

    public class Viewport {
        public Location northeast;
        public Location southwest;
    }

   
    public static GoogleAPIParser parse(String json) {
        return (GoogleAPIParser) System.JSON.deserialize(json, GoogleAPIParser.class);
    }

}

Which I invoke using:

GoogleAPIParser parser = GoogleAPIParser.parse(jsonResponse);

 

How do I go through and access those attributes within the class that is returned now?

Best Answer chosen by Aviator517
Mikola SenykMikola Senyk
After parsing JSON you can work with two properties: List<Results> results and String status.
GoogleAPIParser parser = GoogleAPIParser.parse(jsonResponse);
System.debug('Status: ' + parser.status);
System.debug('Number of results: ' + parser.results.size());
if ( parser.results.size() > 0 ) {
    GoogleAPIParser.Results firstResult =  parser.results[0];
    System.debug('First result location: ' + firstResult.geometry.location);
    ...
}
Be careful with access to inner properties. Some of them can be equal to null.

All Answers

kevin Carotherskevin Carothers
There are people who are much more versed than me that can help you with this, but typically the deserialize method won't work unless the JSON string is parented, which means you might have to "fudge" it a little.   Something like;

jsonResponse = 'GoogleAPIParser[' + jsonResponse + ']';

And, when you call your deserialization, it should (if the variable names and structure match the HTTP service you're consuming)  parse your individual elements into the structure you defined.

Don't forget your setter;getter methods too.
Mikola SenykMikola Senyk
After parsing JSON you can work with two properties: List<Results> results and String status.
GoogleAPIParser parser = GoogleAPIParser.parse(jsonResponse);
System.debug('Status: ' + parser.status);
System.debug('Number of results: ' + parser.results.size());
if ( parser.results.size() > 0 ) {
    GoogleAPIParser.Results firstResult =  parser.results[0];
    System.debug('First result location: ' + firstResult.geometry.location);
    ...
}
Be careful with access to inner properties. Some of them can be equal to null.

This was selected as the best answer
Aviator517Aviator517
Thank you!