Reputation: 605
I'm looking to parse JSONobjects, but I keep getting null when using fromJson. I'm sure inputStream is valid, it looks like the reader is initialized and filled as well, but 'response' keeps coming back null, causing an fatal exception. I am expecting to get a 'response' with 3 fields, one of which a result type list, with the only field in result being the name. What am I doing wrong?
I'm using the following code:
String url = uribuilder.build().toString();
try {
HttpRequest httpRequest = requestFactory.buildGetRequest(new GenericUrl(url));
HttpResponse httpResponse = httpRequest.execute();
inputStream = httpResponse.getContent();
} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
Reader reader = new InputStreamReader(inputStream);
SearchResponse response = gson.fromJson(reader, SearchResponse.class);
List<Result> resultList = response.results;
SearchResponse class:
package com.example.places;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class SearchResponse {
@SerializedName("html_attributions")
public String html_attributions;
public List<Result> results;
@SerializedName("status")
public String status;
}
Result class:
package com.example.places;
import com.google.gson.annotations.SerializedName;
public class Result {
@SerializedName("name")
public String name;
}
This is an example of the JSON inputStream, which I have confirmed is being downloaded in the var inputStream.
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : 52.3784713,
"lng" : 4.629422400000001
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "426ed4299daa6451e2293b1677e06b524562c547",
"name" : "Lange Lakenstraat",
"reference" : "CqQBoAAAANEj46iTeMbRHeUcEGFeaCAMrnHxXvFpadEefjrl4qDBitY4b5c2kjVunQrm496UeU1BHLiflo4tA6z7sDBZw3u0b2oPwTqOSiA1Jf4TZA3J6GeGfo_0tLV5dnHH2a3gJbl7fnDvWdZco2BvP_mgVSJgcC2hnb3H8xf9_HYUKtWjAPiV-lY-TnIeZqJaAaH7rJfg9OuHMjmYVYnYuaW0FoQSEEBP1JORpML1X0D9qgGPl0QaFJhSaAVENN_I4-p8tK-5B790QkwD",
"types" : [ "route" ],
"vicinity" : "Oude Stad"
},
{
"geometry" : {
"location" : {
"lat" : 52.3812134,
"lng" : 4.633607599999999
},
"viewport" : {
"northeast" : {
"lat" : 52.3854271,
"lng" : 4.644829
},
"southwest" : {
"lat" : 52.375918,
"lng" : 4.624482899999999
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "7b7289c46ec49e2de4c7922bb1489f4b7d285385",
"name" : "Centrum",
"reference" : "CpQBjQAAAPTn0HELVmsIds40sY_RGXIY1GmrhqlfejMmrQrG2Gl095VujXOugcPR5ZuZ3-aNZhLZEXNsOO_Ghf_0vEnIkjVan11tb1WtDiwJrIfAa31hPt8XlIGY3JBWKXew0qVpGXZbEoHvhThzn-z0OBZ0pqMR5PZrU7mgoH26pbAR_y-Nngo74sQHZs9wO3dzQl34RRIQMvDj3KMrzUBgSf6WVVHzNRoUlDtZh_8FsLn3qaEWzvPnnyFe9j8",
"types" : [ "sublocality", "political" ],
"vicinity" : "Centrum"
}
],
"status" : "OK"
}
Upvotes: 8
Views: 12891
Reputation: 890
I faced a similar issue, but in my case I had this object:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EmployeeDto {
@ApiModelProperty(notes = "Crew Id")
private String crewId;
@ApiModelProperty(notes = "Employee Role")
private String employeeRole;
@ApiModelProperty(notes = "Employee First Name")
private String firstName;
@ApiModelProperty(notes = "Employee Last Name")
private String lastName;
}
And this was the string from a previous query:
{"crewId":"924142","employeeRole":"Manager","firstName":"Hans","lastName":"Miller"}
The problem was because of camelCase notation, so I changed it to:
{"crew_id":"924142","employee_role":"Manager","first_name":"Hans","last_name":"Miller"}
According with the documentation that field policy can be defined in Gson:
Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
Upvotes: 0
Reputation: 605
Well, strange one. In this case the error came from the SearchResponse class. If you look at the JSON, the first field is
"html_attributions" : [],
So an array.
in SearchResponse
public String html_attributions;
should be
public String[] html_attributions;
The code now shows signs of life. Why it threw a null exception instead of a to be expected "java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY ", I have no idea.
Upvotes: 2