Reputation: 35
Hello guys i need to output a json string like this, I use Java and Jackson.
{"x_axis": {"type": "datetime"},"series": [
{
"name": "Visitors per month",
"data": [
["2014-01", 71173],
["2014-02", 57624],
["2014-03", 64851],
["2014-04", 60486],
["2014-05", 60500],
["2014-06", 62908],
["2014-07", 64818],
["2014-08", 59961],
["2014-09", 58542],
["2014-10", 22050]
],
} ]}
But I tried everything but I get this, i hope you will understand.
{"series": [
{
"data": [
"{2016-02-12 09:00:00.0, 565}",
"{2016-02-12 09:00:00.0, 565}"
],
"name": "Calls per minute"
}],"x_axis": {
"type": "datetime"}}
UPDATE
The problem is I want to output the data like this
"data": [
["2014-01", 71173],
["2014-02", 57624],
["2014-03", 64851],
["2014-04", 60486],
["2014-05", 60500],
["2014-06", 62908],
["2014-07", 64818],
["2014-08", 59961],
["2014-09", 58542],
["2014-10", 22050]
]
And not like this
"data": [
"{2016-02-12, 565}",
"{2016-02-12, 565}"
]
My Method
public class DataItem
{
public String str;
public String count;
@Override
public String toString() {
return "{"+ str + ", " + count + "}"; // Dit roept de parser aan.
}
public DataItem(String Date, String count)
{
this.str = Date;
this.count = count;
}
}
public LineChart getaverageCallsPerMinuteAsLineChart() throws SQLException {
String query = "select date, averageCallsPerMinute from information where date between now() - INTERVAL 1 DAY and now()";
LineChart linechart = new LineChart();
X_Axis x_axis = new X_Axis("datetime");
linechart.setX_axis(x_axis);
ArrayList<Series> seriesArray = new ArrayList<>();
Series series = new Series();
series.setName("Calls per minute");
List<List<Object>> data = new ArrayList<>();
try {
conn = DBConnection.setDBConnection();
statement = conn.createStatement();
rs = statement.executeQuery(query);
while(rs.next()){
List<Object> dataItems;
String date = rs.getString(1);
String calls = rs.getString(2);
if(date != null || calls != null)
{
DataItem di = new DataItem(date, calls);
dataItems = new ArrayList<Object>(Arrays.asList(di));
data.add(dataItems);
}
}
}catch(Exception e){
System.out.println(e);
}finally{
rs.close();
conn.close();
}
series.setData(data);
seriesArray.add(series);
linechart.setSeries(seriesArray);
return linechart;
}
My classes
public class Series {
private String name;
private List<List<Object>> data;
public Series(){}
public Series(String name, List<List<Object>> data){
this.data = data;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<List<Object>> getData() {
return data;
}
public void setData(List<List<Object>> data) {
this.data = data;
}
}
public class X_Axis {
private String type;
public X_Axis(){}
public X_Axis(String type){
this.type = type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
public class LineChart {
private X_Axis x_axis;
private ArrayList<Series> series;
public LineChart(){}
public LineChart(X_Axis x_axis, ArrayList<Series> series){
this.x_axis = x_axis;
this.series = series;
}
public X_Axis getX_axis() {
return x_axis;
}
public void setX_axis(X_Axis x_axis) {
this.x_axis = x_axis;
}
public ArrayList<Series> getSeries() {
return series;
}
public void setSeries(ArrayList<Series> series) {
this.series = series;
}
}
@GET
@Path("/CallsPerMinuteAsLineChart")
@Produces(MediaType.APPLICATION_JSON)
public LineChart CallsPerMinute() throws SQLException {
CompanyDB comp = new CompanyDB();
LineChart linechart = comp.getaverageCallsPerMinuteAsLineChart();
return linechart;
}
Upvotes: 0
Views: 58
Reputation: 737
(I can't post comments yet)
I would say that your :
@Override
public String toString() {
return "{"+ str + ", " + count + "}"; // Dit roept de parser aan.
}
is creating the output with the format "{2016-02-12, 565}"
instead of ["2014-01", 71173]
. The second one, makes me think that they are 2 objects, a String and an Integer, not just one Object (your DataItem class) with 2 attributes and the toString()
function overwritten. I am not very familiar with those libraries but I think you should somehow add
to the JSON twice, first time the time and then the value.
Apart from that, I am checking the formating of the Date, I wil come back when I get something.
EDIT: To get the correct format of the Date I found this:
Date utilDate = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
utilDate = formatter.parse(date);
So I would just change the mask to "yyyy-MM"
Upvotes: 1