Android : Parsing JSON with GSON is not working. Variables are empty

on Sunday, April 19, 2015


I have a JSON string like this:



[
{
"locationID" : "1",
"name" : "Trdinova lokacija",
"address":{
"address1": "Trdinova ulica 9",
"address2": "",
"zipCode": "1000",
"city": "Ljubljana",
"country": "Slovenija"
},
"geoLocation":{
"latitude": 46.05570,
"longitude": 14.50705,
},
"workingTime" : "8:00 - 18:00",
"parkingPlaces" : 10,
"href": "/locations/{id}",
"numberOfCarsOnLocation" : 10
},
{
"locationID" : "2",
"name" : "Dunajska lokacija",
"address":{
"address1": "Dunajska cesta 140",
"address2": "",
"zipCode": "1000",
"city": "Ljubljana",
"country": "Slovenija"
},
"geoLocation":{
"latitude": 46.07980,
"longitude": 14.51309,
},
"workingTime" : "8:00 - 18:00",
"parkingPlaces" : 10,
"href": "/locations/{id}",
"numberOfCarsOnLocation" : 10
}
]


And a Location class like this:



public class Location {
private String locationID;
private String name;
private String address1;
private String address2;
private String zipCode;
private String city;
private String country;
private String workingTime;
private String href;
private String distance;

private double latitude;
private double longitude;

private int parkingPlaces;
private int numberOfCarsOnLocation;
}


I am trying to parse it like this:



locationArrayList = gson.fromJson(responseString, new TypeToken<List<ACLocation>>(){}.getType());


I don't get any errors, but when I try to access values (for example address1), it returns null. Only top level values (like locationID, name...) are working. But if I try to access the lower level values (address1, latitude...) it is returning null.


I am guessing that I can't read the lower level values directly into variables, but how else should this be done correctly?


Thank you!


0 comments:

Post a Comment