Convert Java collections into JSON and JSON to collections

This sample show you how to convert Java collections into JSON string. A collections of fruit is built and converted to json object. Then whereas json object is convert to collections object

Other interesting posts you may like

This is output of program

jsonFruitNames = ["Banana","Apple","Orange","Kiwi"]
jsonFruits = [{"name":"Banana","location":"Cuba","recommend":"Should buy"},{"name":"Apple","location":"USA","recommend":"Should eat"},{"name":"Orange","location":"Holland","recommend":""},{"name":"Kiwi","location":"Sweeden","recommend":""}]
fruit.getName() = Banana
fruit.getLocation() = Cuba
fruit.getRecommend() = Should buy
fruit.getName() = Apple
fruit.getLocation() = USA
fruit.getRecommend() = Should eat
fruit.getName() = Orange
fruit.getLocation() = Holland
fruit.getRecommend() =
fruit.getName() = Kiwi
fruit.getLocation() = Sweeden
fruit.getRecommend() =

I notice that this program uses gson library, please add the below dependency into your project

That’s all on how to convert Java collections into JSON and JSON to collections. If you have any opinion or other way for this, please leave comment for dicussing

Leave a Comment


Please share it if you found this useful
Hide Buttons