输出json字符串,java无法正常工作

时间:2016-02-12 14:35:20

标签: java json jackson

大家好我需要输出像这样的json字符串,我使用Java和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]
  ],
} ]}

但我尝试了一切,但我明白了,我希望你能理解。

{"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"}}

更新

问题是我想输出像这样的数据

 "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]
  ]

不喜欢这个

"data": [
    "{2016-02-12, 565}",
    "{2016-02-12, 565}"
  ]

我的方法

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;
    }

我的班级

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;
}

1 个答案:

答案 0 :(得分:1)

(我无法发表评论)

我会说你的:

@Override
public String toString() {
       return "{"+ str + ", " + count + "}"; // Dit roept de parser aan.
}

正在使用格式"{2016-02-12, 565}"而不是["2014-01", 71173]创建输出。第二个,让我认为它们是2个对象,一个String和一个Integer,而不仅仅是一个具有2个属性和toString()函数被覆盖的Object(您的DataItem类)。我对这些库不是很熟悉,但我认为你应该以某种方式{J}两次{J}两次,第一次是时间,然后是值。

除此之外,我正在检查日期的格式化,当我得到一些东西时我会回来。

编辑:要获得日期的正确格式,我发现了这个:

add

所以我只需将面具更改为Date utilDate = null; SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); utilDate = formatter.parse(date);