Reputation: 8985
I started using the Google Places api by calling the following url
https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Sydney&sensor=true&key=AddYourOwnKeyHere
I found a post here on stackoverflow that showed the the basic structure of classes I need to to parse the json with gson. Here is what I have so far.
public class GoogleMapper
{
@SerializedName("debug_info")
private List<String> debug_info;
@SerializedName("html_attributions")
private List<String> html_attributions;
@SerializedName("next_page_token")
private String next_page_token;
@SerializedName("results")
private List<Results> results;
@SerializedName("status")
private String status;
}
public class Results
{
@SerializedName("formatted_address")
private String formatted_address;
@SerializedName("icon")
private String icon;
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("rating")
private Double rating;
@SerializedName("reference")
private String reference;
@SerializedName("types")
private List<String> types;
}
And here is the main method
public static void main(String[] args)
{
Places p = new Places();
try
{
String page = p.getPage();
Gson gson = new GsonBuilder().serializeNulls().create();
JsonParser parser = new JsonParser();
JsonObject o = (JsonObject)parser.parse(page);
String json = gson.toJson(o);
GoogleMapper mapper = gson.fromJson(json, GoogleMapper.class);
}
catch (Exception e)
{
e.printStackTrace();
}
}
When I run the program I get no errors. But how do I get the parsed data printed?
Upvotes: 1
Views: 1476
Reputation: 6934
Add getters to your classes (and setters if you want, but are not needed)
public class GoogleMapper {
@SerializedName("debug_info")
private List<String> debug_info;
@SerializedName("html_attributions")
private List<String> html_attributions;
@SerializedName("next_page_token")
private String next_page_token;
@SerializedName("results")
private List<Results> results;
public List<String> getDebugInfo() {
return debug_info;
}
public List<String> getHtmlAttributions() {
return html_attributions;
}
public String getNextPageToken() {
return next_page_token;
}
public List<Results> getResults() {
return results;
}
public String getStatus() {
return status;
}
@SerializedName("status")
private String status;
}
public class Results {
public String getFormattedAddress() {
return formatted_address;
}
public String getIcon() {
return icon;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Double getRating() {
return rating;
}
public String getReference() {
return reference;
}
public List<String> getTypes() {
return types;
}
@SerializedName("formatted_address")
private String formatted_address;
@SerializedName("icon")
private String icon;
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("rating")
private Double rating;
@SerializedName("reference")
private String reference;
@SerializedName("types")
private List<String> types;
}
and then do like this in your main:
Gson gson = new GsonBuilder().serializeNulls().create();
GoogleMapper mapper = gson.fromJson(page, GoogleMapper.class);
List<Results> results = mapper.getResults();
if(results != null){
for(Results r : results){
System.out.println("Fetched name: " + r.getName());
}
}
Notes:
page
string to the Gson instanceResults
is plural, I think that Result
is a better name, but I left as you wroteUpvotes: 2