简体   繁体   中英

How can I create this JsonObject using Gson API?

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 :)

Its pretty straight forward, take a look at this :

Java Arraylist Data extraction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM