如何使用 Gson API 创建这个 JsonObject?
我在正确序列化/反序列化这个 json 对象时遇到了问题:
<前><代码>{ 系列:[{ 数据:[[长整型],...], 标签:字符串 }, ... ], 选项:{ 鼠标:{轨迹:true}, x轴:{noTicks:10}, 点:{显示:true}, 行:{显示:true} } }
这是我尝试过的;然而,Gson 似乎不喜欢 fromJson,尽管它确实正确地执行了 toJson。
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; }
}
任何提示或答案表示赞赏。谢谢 :)
I've been having trouble getting this json object to serialize/deserialize properly:
{ series:[{ data:[[long,int], ... ], label:String }, ... ], options:{ mouse:{track:true}, xaxis:{noTicks:10}, points:{show:true}, lines:{show:true} } }
Here is what I tried; however, Gson doesn't seem to like doing fromJson, although, it does correctly do toJson.
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; }
}
Any hints or answers are appreciated. Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它非常简单,看看这个:
Java Arraylist 数据提取
Its pretty straight forward, take a look at this :
Java Arraylist Data extraction