I want following Code as an output:
Required Output:
{
"dataProvider" : [ {
"year" : 2009,
"income" : 21.4,
"expenses" : 20.3
}, {
"year" : 2010,
"income" : 31.3,
"expenses" : 43.1
}, {
"year" : 2011,
"income" : 15.6,
"expenses" : 9.8
}, {
"year" : 2012,
"income" : 34.8,
"expenses" : 34.7
}, {
"year" : 2013,
"income" : 20.1,
"expenses" : 24.4,
"dashLengthLine" : 5
}, {
"year" : 2014,
"income" : 27.3,
"expenses" : 21.9,
"dashLengthLine" : 0,
"dashLengthColumn" : 5,
"alpha" : 0.5,
"additional" : "(projection)"
} ],
"graphs" : [ {
"alphaField" : "alpha",
"balloonText" : "<span style='font-size:12px;'>[[title]] in [[category]]:<br><span style='font-size:20px;'>[[value]]</span> [[additional]]</span>",
"fillAlphas" : 1,
"title" : "Income",
"type" : "column",
"valueField" : "income",
"dashLengthField" : "dashLengthColumn"
}, {
"balloonText" : "<span style='font-size:12px;'>[[title]] in [[category]]:<br><span style='font-size:20px;'>[[value]]</span> [[additional]]</span>\t",
"title" : "Expenses",
"valueField" : "expenses",
"dashLengthField" : "dashLengthLine",
"id" : "graph2",
"bullet" : "round",
"lineThickness" : 3,
"bulletSize" : 7,
"bulletBorderAlpha" : 1,
"bulletColor" : "#FFFFFF",
"useLineColorForBulletBorder" : true,
"bulletBorderThickness" : 3,
"lineAlpha" : 1
} ]
}
Here is what i have tried so far:
MainClass.java
public static void main(String args[]) throws JsonProcessingException {
List<Map<Object, Object>> dataProviderList = new LinkedList<>();
List<Object> yearList = new LinkedList<>();
List<Object> incomeList = new LinkedList<>();
List<Object> expensesList = new LinkedList<>();
yearList.add(2009);
yearList.add(2010);
yearList.add(2011);
yearList.add(2012);
yearList.add(2013);
yearList.add(2014);
incomeList.add(21.4);
incomeList.add(31.3);
incomeList.add(15.6);
incomeList.add(34.8);
incomeList.add(20.1);
incomeList.add(27.3);
expensesList.add(20.3);
expensesList.add(43.1);
expensesList.add(9.8);
expensesList.add(34.7);
expensesList.add(24.4);
expensesList.add(21.9);
for (int i = 0; i < yearList.size(); i++) {
Map<Object, Object> dataProviderValueMap = new LinkedHashMap<>();
dataProviderValueMap.put("year", yearList.get(i));
dataProviderValueMap.put("income", incomeList.get(i));
dataProviderValueMap.put("expenses", expensesList.get(i));
if (i == yearList.size() - 1) {// for year 2014
dataProviderValueMap.put("dashLengthLine", 0);
dataProviderValueMap.put("dashLengthColumn", 5);
dataProviderValueMap.put("dashLengthColumn", 5);
dataProviderValueMap.put("alpha", 0.5);
dataProviderValueMap.put("additional", "(projection)");
}
if (i == yearList.size() - 2) {// for year 2103
dataProviderValueMap.put("dashLengthLine", 5);
}
dataProviderList.add(dataProviderValueMap);
}
// graphs
List<GraphPojo> GraphPojo = new ArrayList<GraphPojo>();
GraphPojo GraphPojo1 = new GraphPojo();
GraphPojo1 = new GraphPojo();
GraphPojo1.setAlphaField("alpha");
GraphPojo1.setBalloonText(
"<span style='font-size:12px;'>[[title]] in [[category]]:<br><span style='font-size:20px;'>[[value]]</span> [[additional]]</span>");
GraphPojo1.setFillAlphas(1);
GraphPojo1.setTitle("Income");
GraphPojo1.setType("column");
GraphPojo1.setValueField("income");
GraphPojo1.setDashLengthField("dashLengthColumn");
GraphPojo.add(GraphPojo1);
GraphPojo1 = new GraphPojo();
GraphPojo1.setId("graph2");
GraphPojo1.setBalloonText(
"<span style='font-size:12px;'>[[title]] in [[category]]:<br><span style='font-size:20px;'>[[value]]</span> [[additional]]</span> ");
GraphPojo1.setBullet("round");
GraphPojo1.setLineThickness(3);
GraphPojo1.setBulletSize(7);
GraphPojo1.setBulletBorderAlpha(1);
GraphPojo1.setBulletColor("#FFFFFF");
GraphPojo1.setUseLineColorForBulletBorder(true);
GraphPojo1.setBulletBorderThickness(3);
GraphPojo1.setFillAlphas(0);
GraphPojo1.setLineAlpha(1);
GraphPojo1.setTitle("Expenses");
GraphPojo1.setValueField("expenses");
GraphPojo1.setDashLengthField("dashLengthLine");
GraphPojo.add(GraphPojo1);
//responsePojo.setGraphPojo(GraphPojo);
Map<String, Object> responseMap = new LinkedHashMap<>();
responseMap.put("dataProvider", dataProviderList);
responseMap.put("graphs", GraphPojo);
// Jackson logic to convert map into json
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
String mapToJson = objectMapper.writeValueAsString(responseMap);
System.out.println(mapToJson);
}
}
here is the pojo class for graphs:
GraphPojo.java
package PojoClasses;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class GraphPojo {
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String alphaField;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String balloonText;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public int fillAlphas;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String title;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String type;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String valueField;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String dashLengthField;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String id;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String bullet;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public int lineThickness;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public int bulletSize;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public int bulletBorderAlpha;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String bulletColor;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public boolean useLineColorForBulletBorder;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public int bulletBorderThickness;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public int lineAlpha;
@JsonSerialize(include = JsonSerialize.Inclusion.NON_DEFAULT)
public String getAlphaField() {
return alphaField;
}
public void setAlphaField(String alphaField) {
this.alphaField = alphaField;
}
public String getBalloonText() {
return balloonText;
}
public void setBalloonText(String balloonText) {
this.balloonText = balloonText;
}
public int getFillAlphas() {
return fillAlphas;
}
public void setFillAlphas(int fillAlphas) {
this.fillAlphas = fillAlphas;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValueField() {
return valueField;
}
public void setValueField(String valueField) {
this.valueField = valueField;
}
public String getDashLengthField() {
return dashLengthField;
}
public void setDashLengthField(String dashLengthField) {
this.dashLengthField = dashLengthField;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBullet() {
return bullet;
}
public void setBullet(String bullet) {
this.bullet = bullet;
}
public int getLineThickness() {
return lineThickness;
}
public void setLineThickness(int lineThickness) {
this.lineThickness = lineThickness;
}
public int getBulletSize() {
return bulletSize;
}
public void setBulletSize(int bulletSize) {
this.bulletSize = bulletSize;
}
public int getBulletBorderAlpha() {
return bulletBorderAlpha;
}
public void setBulletBorderAlpha(int bulletBorderAlpha) {
this.bulletBorderAlpha = bulletBorderAlpha;
}
public String getBulletColor() {
return bulletColor;
}
public void setBulletColor(String bulletColor) {
this.bulletColor = bulletColor;
}
public boolean isUseLineColorForBulletBorder() {
return useLineColorForBulletBorder;
}
public void setUseLineColorForBulletBorder(boolean useLineColorForBulletBorder) {
this.useLineColorForBulletBorder = useLineColorForBulletBorder;
}
public int getBulletBorderThickness() {
return bulletBorderThickness;
}
public void setBulletBorderThickness(int bulletBorderThickness) {
this.bulletBorderThickness = bulletBorderThickness;
}
public int getLineAlpha() {
return lineAlpha;
}
public void setLineAlpha(int lineAlpha) {
this.lineAlpha = lineAlpha;
}
}
Here is my trial OutPut:
Trial output for dataProvider
{
"dataProvider" : [ {
"year" : 2009,
"income" : 21.4,
"expenses" : 20.3
}, {
"year" : 2010,
"income" : 31.3,
"expenses" : 43.1
}, {
"year" : 2011,
"income" : 15.6,
"expenses" : 9.8
}, {
"year" : 2012,
"income" : 34.8,
"expenses" : 34.7
}, {
"year" : 2013,
"income" : 20.1,
"expenses" : 24.4,
"dashLengthLine" : 5
}, {
"year" : 2014,
"income" : 27.3,
"expenses" : 21.9,
"dashLengthLine" : 0,
"dashLengthColumn" : 5,
"alpha" : 0.5,
"additional" : "(projection)"
} ],
Trial Out put for graphs
{
"graphs" : [ {
"alphaField" : "alpha",
"balloonText" : "<span style='font-size:12px;'>[[title]] in [[category]]:<br><span style='font-size:20px;'>[[value]]</span> [[additional]]</span>",
"fillAlphas" : 1,
"title" : "Income",
"type" : "column",
"valueField" : "income",
"dashLengthField" : "dashLengthColumn"
}, {
"balloonText" : "<span style='font-size:12px;'>[[title]] in [[category]]:<br><span style='font-size:20px;'>[[value]]</span> [[additional]]</span>\t",
"title" : "Expenses",
"valueField" : "expenses",
"dashLengthField" : "dashLengthLine",
"id" : "graph2",
"bullet" : "round",
"lineThickness" : 3,
"bulletSize" : 7,
"bulletBorderAlpha" : 1,
"bulletColor" : "#FFFFFF",
"useLineColorForBulletBorder" : true,
"bulletBorderThickness" : 3,
"lineAlpha" : 1
} ]
}
how can I Get this Json Snippet by using dynamic for loop including "If condition" ?
Is there any other way to make it more dynamic ? As well as I want to use the for loop and if condition for graphs aslo. Because I do not want to use pojo class in it.
I was trying to find the solution for long time but could not find any proper solution Please help me with this doubt.
Thanks in advance !!
Aucun commentaire:
Enregistrer un commentaire