• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert ArrayList of custom objects to JSON  RSS feed

 
Thomas Greene
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList of custom Objects. Each of these objects have an arraylist of another custom object. Then these second level of custom objects have an arraylist of another custom object.

This is how the class hierarchy looks like







I have an arraylist of Undle objects



I would like to convert Undle ArrayList to a JSON. How can I flatten this hierarcical structure of beans and put it in a JSON?

I tried doing something like



and



But it seems that they work only if a String ArrayList is passed.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What JSON library are you using?

Are you converting just the list or is the list inside a class?



I found the Jackson library fro JSON conversion pretty simple to use. Yet converting a list to JSON in itself is a bit tricky.
 
Thomas Greene
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to covert list inside the list. I am using json-simple but now also considering gson library

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well not that familiar with either one but for Jackson, it utilizes the JavaType to help it determine what the T is for the List<T>

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!