如何使用 Gson API 创建这个 JsonObject?

发布于 2024-10-31 06:48:41 字数 1130 浏览 1 评论 0原文

我在正确序列化/反序列化这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆で 2024-11-07 06:48:41

它非常简单,看看这个:

Java Arraylist 数据提取

Its pretty straight forward, take a look at this :

Java Arraylist Data extraction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文