我有一个像这样的JSON字符串:
[
{
"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
}
]
这样的位置类:
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;
}
我试图像这样解析它:
locationArrayList = gson.fromJson(responseString, new TypeToken<List<ACLocation>>(){}.getType());
我没有收到任何错误,但是当我尝试访问值(例如address1)时,它返回null。只有顶级值(如locationID,name ...)才有效。但是,如果我尝试访问较低级别的值(address1,纬度...),则返回null。
我猜我不能直接将较低级别的值读入变量,但是如何才能正确地完成这些?
谢谢!
答案 0 :(得分:0)
您的address1,address2 ...字段位于JSON对象中,因此在Location
类中,您必须具有address
属性,如:
public class Location {
private String locationID;
private String name;
private Address address
...
}
Address
类看起来像:
public class Address {
private String address1;
private String address2;
private String zipCode;
private String city;
private String country;
}
geoLocation
字段的推理相同。