Creating JSON Object

22 views
Skip to first unread message

Kyle

unread,
Apr 12, 2011, 3:55:52 AM4/12/11
to google-gson

Hey everyone, so I have been dealing with an issue deserializing my
object. I get the feeling that I am not creating the object correctly,
so here is what I have.

The structure I am attempting to put the data into follows this style.

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


My attempt at doing this looks something like this:

class JsonBlock {
List<Series> series = new ArrayList<Series>();
}

class Series {
List<long[]> data = new ArrayList<long[]>();
String label = "";
public Series(String label, long[] d) { this.label = label;
data.add(d); }
}

inde...@gmail.com

unread,
Apr 12, 2011, 4:20:00 PM4/12/11
to google-gson
I guess you need a top-level class too:
Class TopLevel {
Series series;
Options options;
}

Other than that I dont see a problem with your code. What is the
specific error you are running into? Would help if you can share a
JUnit test that we can reproduce.

Inder
Message has been deleted

Kyle

unread,
Apr 12, 2011, 5:23:43 PM4/12/11
to google-gson
Would you say JsonBlock class is that top level class? I have not put
in the options yet. Also, does Gson support deserializing
List<Object>???

Joel

unread,
Apr 12, 2011, 7:30:22 PM4/12/11
to google-gson
It looks like JsonBlock as a top-level class should be sufficient. I
noticed that it is missing an options field, so the options structure
would simply be ignored in your case.

Are you actually running into problems?

Kyle

unread,
Apr 12, 2011, 7:43:52 PM4/12/11
to google-gson
It is having trouble deserializing the list of Series objects.

inde...@gmail.com

unread,
Apr 12, 2011, 7:50:57 PM4/12/11
to google-gson
Gson would not be able to deserialize into List<Object> since it
doesn't know how to create a concrete class for "Object".

Inder


On Apr 12, 2:23 pm, Kyle <frigh...@gmail.com> wrote:
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Kyle

unread,
Apr 12, 2011, 8:52:04 PM4/12/11
to google-gson
Okay, so apparently 1.7 fixed the problem... thanks :D I was really
close to being done though! I worked on that error for days and to
have it fixed after someone actually tried to help me :P (I asked soo
many ppl in so many places :P)

Inderjeet Singh

unread,
Apr 13, 2011, 2:02:30 AM4/13/11
to googl...@googlegroups.com, Kyle
Good to know that it worked for you. In general Gson can not
deserialize into Object type except if it is a primitive.
We did improve handling of certain edge cases around that (when it is
List or Map of Objects) so that is probably what made it work for you.

Inder

> --
> You received this message because you are subscribed to the Google Groups "google-gson" group.
> To post to this group, send email to googl...@googlegroups.com.
> To unsubscribe from this group, send email to google-gson...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages