【发布时间】:2017-05-01 11:40:37
【问题描述】:
这是我来自服务器的 JSON 对象:
"driverInfo": {
"rideTime": null,
"rideId": 1105,
"driverProfile": {
"userId": 9,
"firstname": "nawaraj",
"lastname": "shrestha",
"phone": "9841444444",
"email": null,
"location": {
"lat": 27.7131382,
"lng": 85.3255354
},
"vehicleType": "Taxi",
"vehicleNo": "4556",
"rating": 3.3,
"imageName": "NAbin25",
"driverId": 4
},
"destinationLocation": null,
"myLocation": {
"lat": 27.7131382,
"lng": 85.3255354
},
"otpCode": "7857",
"trackRideUrl": null,
"customerId": 1
}
这是我的java类
public class RideDetails implements Serializable{
@Nullable
private String rideTime;
private Integer rideId;
private DriverProfile driverProfile;
@Nullable
private LatLng destinationLocation;
@Nullable
private LatLng myLocation;
private String otpCode;
private String trackRideUrl;
private Integer customerId;
public Integer getRideId() {
return rideId;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public void setRideId(Integer rideId) {
this.rideId = rideId;
}
public DriverProfile getDriverProfile() {
return driverProfile;
}
public void setDriverProfile(DriverProfile driverProfile) {
this.driverProfile = driverProfile;
}
@Nullable
public LatLng getDestinationLocation() {
return destinationLocation;
}
public void setDestinationLocation(@Nullable LatLng destinationLocation) {
this.destinationLocation = destinationLocation;
}
@Nullable
public LatLng getMyLocation() {
return myLocation;
}
public void setMyLocation(@Nullable LatLng myLocation) {
this.myLocation = myLocation;
}
@Nullable
public String getRideTime() {
return rideTime;
}
public void setRideTime(@Nullable String rideTime) {
this.rideTime = rideTime;
}
public String getOtpCode() {
return otpCode;
}
public void setOtpCode(String otpCode) {
this.otpCode = otpCode;
}
public String getTrackRideUrl() {
return trackRideUrl;
}
public void setTrackRideUrl(String trackRideUrl) {
this.trackRideUrl = trackRideUrl;
}
}
为了映射对象.. 我使用 Gson 是这样的:
Gson gson = new Gson();
RideDetails rideDetails = gson.fromJson(jsonObject.toString(),RideDetails.class);
JSON 对象的所有细节都可以映射到 Java 类,除了 LatLng。
虽然LatLng 在来自服务器的 JSON 对象中有一个值。所有三个LatLng:location、destinationLocation 和myLocation 在映射后都有值{lat:0.0,lng:0.0}。可能是什么问题?
【问题讨论】:
-
使用jsonschema2pojo.org生成精确的POJO类