【问题标题】:JSON object not mapping to Java object with GsonJSON 对象未使用 Gson 映射到 Java 对象
【发布时间】: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}。可能是什么问题?

【问题讨论】:

标签: java android gson


【解决方案1】:

来自Google APIs 的LatLng 对象确实包含lat 和lng 参数。这意味着您的对象没有映射。

使用正确的参数创建您自己的Location 对象,以确保正确的映射。或者确保 json 包含 latitude 和 longitude 而不是 lat 和 lng,以便它可以自动映射到 LatLng 对象。

另外,层次结构是错误的。 myLocation、destinationLocation 和 location 应该映射到 DriverProfile 下。

【讨论】:

  • 可以请详细说明“确保gson有纬度和经度而不是lat和lng”..这可能吗?
  • 嗯,你说你的服务器提供你的 JSON。我假设您可以要求您的 API 人员更改 JSON :)。
【解决方案2】:

位置应该是 DriverProfile 对象的一部分。

【讨论】:

    【解决方案3】:

    LatLng 类的字段 latitude 和 longitude 不同于您的服务器作为 JSON 的一部分发送的字段 lat 和 lng。

    【讨论】:

    • 谢谢你..这正是问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 2019-02-06
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多