将JSON映射到Class不起作用

时间:2017-10-05 22:35:17

标签: java json mongodb jackson jongo

我有一个包含其他类的其他属性的类,当我尝试从json转换为我的类时,会显示错误。

这是我的班级:

import org.jongo.marshall.jackson.oid.MongoObjectId;
import org.json.JSONObject;

import java.util.List;

public class BusinessTravelDTO {
  @MongoObjectId
  private String id;

  private String travelerId;

  private BusinessTravelStatus status;

  List<FlightDTO> flights;

  List<HotelDTO> hotels;

  List<CarDTO> cars;

  public BusinessTravelDTO() { }

  public BusinessTravelDTO(JSONObject data) {
    this.travelerId = data.getString("travelerId");
    this.status =  BusinessTravelStatus.valueOf(data.getString("status"));
    this.flights = HandlerUtil.getInputFlights(data.getJSONArray("flights"));
    this.hotels = HandlerUtil.getInputHotels(data.getJSONArray("hotels"));
    this.cars = HandlerUtil.getInputCars(data.getJSONArray("cars"));
  }

  public JSONObject toJson() {
    return new JSONObject()
            .put("id", this.id)
            .put("travelerId", this.travelerId)
            .put("status", this.status)
            .put("flights", this.flights)
            .put("hotels", this.hotels)
            .put("cars", this.cars);
  }

这是我尝试转换为课程的地方:

 public static JSONObject acceptBusinessTravel(JSONObject input) {
    String btId = getStringField(input, "id");
    MongoCollection businessTravels = getBTCollection();

     // Here is the problem...
    BusinessTravelDTO bt = businessTravels.findOne(new ObjectId(btId)).as(BusinessTravelDTO.class);
    bt.setStatus(BusinessTravelStatus.Accepted);

    businessTravels.save(bt);

    return new JSONObject().put("message", "The business travel has been ACCEPTED by your manager. Check your email.");
  }

以下是我收到的错误:

"error": "org.jongo.marshall.MarshallingException: Unable to unmarshall result to class path.data.BusinessTravelDTO from content { \"_id\" : { \"$oid\" : \"59d6905411d58632fd5bd8a5\"} , \"travelerId\" 

在jongo docs中指定该类应该有一个空构造函数... http://jongo.org/#mapping我有2个构造函数,我也尝试过@JsonCreator,但没有成功... :(
你知道它为什么不能转换吗?它可能与BusinesTravelDTO中的字段相关,例如List CarDTO for ex?

1 个答案:

答案 0 :(得分:0)

我最终找到了解决方案;

所有类FlightDTO,HotelDTO,CarDTO都需要一个空构造函数,我应该重写toJson方法如下:

public JSONObject toJson() {
    JSONObject obj = new JSONObject().put("id", this.id).put("travelerId", this.travelerId).put("status", this.status);

    if (flights != null) {
      JSONArray flightArray = new JSONArray();
      for (int i = 0; i < flights.size(); ++i) {
        flightArray.put(flights.get(i).toJson());
      }
      obj.put("flights", flightArray);
    }

    if (hotels != null) {
      JSONArray hotelArray = new JSONArray();
      for (int i = 0; i < hotels.size(); ++i) {
        hotelArray.put(hotels.get(i).toJson());
      }
      obj.put("hotels", hotelArray);
    }

    if (cars != null) {
      JSONArray carArray = new JSONArray();
      for (int i = 0; i < cars.size(); ++i) {
        carArray.put(cars.get(i).toJson());
      }
      obj.put("cars", carArray);
    }
    return obj;
  }

这是FlightDTO;

public class FlightDTO {
  @MongoObjectId
  private String id;

  private String departure;
  private String arrival;
  private String airline;
  private Double price;

  public FlightDTO() {
  }

  public FlightDTO(JSONObject data) {
    this.departure = data.getString("departure");
    this.arrival = data.getString("arrival");
    this.airline = data.getString("airline");
    this.price = data.getDouble("price");
  }

  public JSONObject toJson() {
    return new JSONObject()
            .put("id", this.id)
            .put("departure", this.departure)
            .put("arrival", this.arrival)
            .put("airline", this.airline)
            .put("price", this.price);
  }
}

现在效果很好! :)