Reputation: 713
I have the following output that i need to send to server from my application, but I don't know how to create it as a POJO. JSON structure is as follows:
[
{
"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": ""
}
]
}
]
I cannot figure out how to create [] that 2 parantheses from my pojo to encapsulate all other fields.
Thanks for the help,
Upvotes: 0
Views: 811
Reputation: 13029
Like so :
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
}
And serialize a WrapperArray like that :
ObjectMapper mapper = new ObjectMapper();
WrapperArray wrapperArray = new WrapperArray();
wrapperArray.add(dataWrapper1);
wrapperArray.add(dataWrapper2);
String jsonResult = mapper.writeValueAsString(wrapperArray);
Upvotes: 1