我需要从我的应用程序发送到服务器的以下输出,但我不知道如何将其创建为POJO。 JSON结构如下:
[
{
"data_type": "gen_shifts",
"data": [
{
"device_id": "121212",
"shift_type": "12",
"start_time": "",
"end_time": "",
"staff_card_no": "",
"processed_date": ""
},
{
"device_id": "121212",
"shift_type": "12",
"start_time": "",
"end_time": "",
"staff_card_no": "",
"processed_date": ""
}
]
},
{
"data_type": "charge",
"data": [
{
"card_no": "121212",
"current_balance": "12",
"sam_signature": "243453453453"
},
{
"card_no": "7777",
"current_balance": "32",
"sam_signature": "243453453453"
}
]
},
{
"data_type": "shifts_types",
"data": [
{
"type_id": "121212",
"type_name": "12",
"start_time": "243453453453",
"end_time": ""
},
{
"type_id": "121212",
"type_name": "12",
"start_time": "243453453453",
"end_time": ""
}
]
}
]
我无法弄清楚如何从我的pojo创建[] 2个parantheses来封装所有其他字段。
感谢您的帮助,
答案 0 :(得分:1)
像这样:
public class WrapperArray extends ArrayList<DataWrapper> {
}
public class DataWrapper {
@JsonProperty(value = "data_type")
private String dataType;
@JsonProperty(value = "data")
private List<Data> data;
// you need to add empty constructor and getters/setters
}
public abstract class Data {
}
public class GenShiftsData extends Data {
@JsonProperty(value = "device_id")
private String deviceId;
@JsonProperty(value = "shift_type")
private String shiftType;
@JsonProperty(value = "start_time")
private String startTime;
@JsonProperty(value = "end_time")
private String endTime;
@JsonProperty(value = "staff_card_no")
private String staffCardNo;
@JsonProperty(value = "processed_date")
private String processedDate;
// you need to add empty constructor and getters/setters
}
并像这样序列化一个WrapperArray:
ObjectMapper mapper = new ObjectMapper();
WrapperArray wrapperArray = new WrapperArray();
wrapperArray.add(dataWrapper1);
wrapperArray.add(dataWrapper2);
String jsonResult = mapper.writeValueAsString(wrapperArray);