将 JSON 映射到 Class 无效
Mapping JSON to Class not working
我有一个 class,其中包含另一个 classes 的其他一些属性,当我尝试从 json 转换为我的 class 时,出现错误显示。
这是我的class:
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);
}
这里是我尝试转换为 class:
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 文档中指定 class 应该有一个空构造函数... http://jongo.org/#mapping 我有 2 个构造函数,我也尝试过 @JsonCreator,但没有成功...: (
你知道为什么它不转换吗?它可能与 BusinesTravelDTO 内部的字段相关,例如 List CarDTO for ex 吗?
我终于找到了解决方案;
所有类 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);
}
}
现在效果很好! :)
我有一个 class,其中包含另一个 classes 的其他一些属性,当我尝试从 json 转换为我的 class 时,出现错误显示。
这是我的class:
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);
}
这里是我尝试转换为 class:
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 文档中指定 class 应该有一个空构造函数... http://jongo.org/#mapping 我有 2 个构造函数,我也尝试过 @JsonCreator,但没有成功...: (
你知道为什么它不转换吗?它可能与 BusinesTravelDTO 内部的字段相关,例如 List CarDTO for ex 吗?
我终于找到了解决方案;
所有类 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);
}
}
现在效果很好! :)