从 jsp 到 ajax cal 我将 json 字符串传递给服务器,并且我正在转换为 json 对象。如何将 jsonobject 转换为 java 中的模型类对象?
在服务器中,我正在这样做:
HttpServletRequest request = ServletActionContext.getRequest();
String jsonData = request.getParameter("JsonData");
JSONObject jsonDataObject = (JSONObject) JSONSerializer.toJSON( jsonData );
我的模型类如下所示:
public class Vehicles {
private List<Vehicle> vehicle;
public List<Vehicle> getVehicle() {
return vehicle;
}
public void setVehicle(List<Vehicle> vehicle) {
this.vehicle= vehicle;
}
}
和
public class Vehicle{
private Integer vId;
private String VName;
private List<Department> department;
//getters and setters;
}
和
public class Department{
private Integer depId;
private String departmentName;
private List<Item> item;
//getters and setters
}
和
public class Item{
private Integer itemId;
private String itemName;
//getters and setters
}
我得到 jsonData String 作为
{"vehicles":[{"vehicle":[{"department":[{"Item":[{"itemId":31,"itemName":"c7"},{"itemId":32,"itemName":"c2"}],"depId":21,"departmentName":"d1"}],"vId":11,"VName":"aaa"},{"department":[{"Item":[{"itemId":33,"itemName":"c3"},{"itemId":34,"itemName":"c4"}],"depId":22,"departmentName":"d2"},{"Item":[{"itemId":36,"itemName":"c1"}],"depId":24,"departmentName":"d3"}],"vId":12,"VName":"bbbb"},{"department":[{"Item":[{"itemId":30,"itemName":"c6"},{"itemId":35,"itemName":"c5"}],"depId":23,"departmentName":"d4"}],"vId":13,"VName":"cccc"},{"department":[{"Item":[{"itemid":37,"itemName":"c8","status":0}],"depId":25,"departmentName":"d5"}],"vId":14,"VName":"ddd"}]}]}
如何在 java 中将 JSONObject jsonDataObject(或 String jsonData)转换为模型类对象(即车辆)?