Reputation: 3030
Class EntityDataSet {
public List<EntityRow> getData() {
return rowList;
}
class EntityRow {
private Map<String, Object> rowData;
public Map<String, Object> getRowData() {
return rowData;
}
}
I wrote a method in restful controller like below.
@RequestMapping(value = "/entityData", method = RequestMethod.POST, headers = "Accept=application/json")
public EntityDataSet getEntityData(@RequestBody QueryObject query) throws Exception{
return entityManager.generateEntityDataByName(query);
}
The returned json looked like below.
{
"data": [
{
"rowData": {
"FISCAL_YEAR_END": null,
"HOME_COUNTRY": null
}
},
{
"rowData": {
"FISCAL_YEAR_END": null,
"HOME_COUNTRY": "10"
}
},
..................
But what I want to get is like below.
{
"data": [
{
"FISCAL_YEAR_END": null,
"HOME_COUNTRY": null
},
{
"FISCAL_YEAR_END": null,
"HOME_COUNTRY": "10"
},
How can I implement it? thanks.
Upvotes: 0
Views: 56
Reputation: 6444
It can be done (if using Jackson) using the @JsonAnySetter
and @JsonAnyGetter
annotations in your EntityRow class:
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
public class EntityDataSet {
@JsonProperty("data")
private List<EntityRow> rowList;
/**
* @return the rowList
*/
public List<EntityRow> getRowList() {
return rowList;
}
/**
* @param rowList the rowList to set
*/
public void setRowList(List<EntityRow> rowList) {
this.rowList = rowList;
}
/**
*
* @param row
*/
public void add(EntityRow row){
if(this.rowList == null){
this.rowList = new ArrayList<EntityRow>();
}
this.rowList.add(row);
}
}
This is the key class, the EntityRow with the @JsonAnySetter and @JsonAnyGetter annotations
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
public class EntityRow {
private Map<String, Object> rowData;
/**
* @param rowData the rowData to set
*/
@JsonAnySetter
public void setRowData(Map<String, Object> rowData) {
this.rowData = rowData;
}
@JsonAnyGetter
public Map<String, Object> getRowData() {
return rowData;
}
}
A main to test it:
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test4 {
public static String getJsonString(Object o){
ObjectMapper mapper = new ObjectMapper();
//For testing
try {
//Convert object to JSON string
String jsonInString = mapper.writeValueAsString(o);
//System.out.println(jsonInString);
return jsonInString;
} catch (JsonProcessingException e){
e.printStackTrace();
}
return null;
}
public static void main(String args[]){
Map<String, Object> map = new HashMap<String, Object>();
map.put("FISCAL_YEAR_END", null);
map.put("HOME_COUNTRY", null);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("FISCAL_YEAR_END", null);
map2.put("HOME_COUNTRY", "10");
EntityRow row1 = new EntityRow();
row1.setRowData(map);
EntityRow row2 = new EntityRow();
row2.setRowData(map2);
EntityDataSet entityDataSet = new EntityDataSet();
entityDataSet.add(row1);
entityDataSet.add(row2);
System.out.println(Test4.getJsonString(entityDataSet));
}
}
Finally, this is the output:
{"data":
[
{
"FISCAL_YEAR_END":null,
"HOME_COUNTRY":null
},
{
"FISCAL_YEAR_END":null,
"HOME_COUNTRY":"10"
}
]
}
Upvotes: 1