如何使用Gson API创建此JsonObject?

时间:2011-04-11 09:00:21

标签: java json api serialization gson

我一直无法让这个json对象正确序列化/反序列化:

{
  series:[{
      data:[[long,int], ... ],
      label:String
  },
  ...
  ],
  options:{
      mouse:{track:true},
      xaxis:{noTicks:10},
      points:{show:true},
      lines:{show:true}
  }
}

这是我尝试过的;然而,Gson似乎并不喜欢从json那里做,虽然它确实正确地做了Json。

class JsonBlock {
    ArrayList<Series> series = new ArrayList();
    String options = "mouse:{track:true},"
                   + "xaxis:{noTicks:10},"
                   + "points:{show:true},"
                   + "lines:{show:true}";
    public String toString() { return String.format("series:%s,options:%s",series, options); }
}

class Series {
    ArrayList<Data> data = new ArrayList();
    String label = "";
    public Series(String label, Data d) { this.label = label; data.add(d); }
    public String toString() { return String.format("data:%s,label:%s", data, label); }
}

class Data {
    long date = -1;
    int qty = 0;
    public Data(long date, int qty) { this.date = date; this.qty = qty; }
    public String toString() { return date + "," + qty; }
}

任何提示或答案都表示赞赏。谢谢:))

1 个答案:

答案 0 :(得分:1)

非常直截了当,看看这个:

Java Arraylist Data extraction