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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.javabycode.json; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class CollectionToJsonSample { public static void main(String[] args) { List<String> fruitNames = new ArrayList<String>(); fruitNames.add("Banana"); fruitNames.add("Apple"); fruitNames.add("Orange"); fruitNames.add("Kiwi"); Gson gson = new Gson(); /*Converts a collection of string object into JSON string.*/ String jsonFruitNames = gson.toJson(fruitNames); System.out.println("jsonFruitNames = " + jsonFruitNames); Fruit banana = new Fruit("Banana", "Cuba", "Should buy"); Fruit apple = new Fruit("Apple", "USA", "Should eat"); Fruit orange = new Fruit("Orange", "Holland", ""); Fruit kiwi = new Fruit("Kiwi", "Sweeden", ""); List<Fruit> fruits = new ArrayList<Fruit>(); fruits.add(banana); fruits.add(apple); fruits.add(orange); fruits.add(kiwi); gson = new Gson(); /*Converts a collection Fruit object into JSON string*/ String jsonFruits = gson.toJson(fruits); System.out.println("jsonFruits = " + jsonFruits); /*Converts JSON string into a collection of Fruit object*/ Type type = new TypeToken<List<Fruit>>() {}.getType(); List<Fruit> fruitList = gson.fromJson(jsonFruits, type); for (Fruit fruit : fruitList) { System.out.println("fruit.getName() = " + fruit.getName()); System.out.println("fruit.getLocation() = " + fruit.getLocation()); System.out.println("fruit.getRecommend() = " + fruit.getRecommend()); } } private static class Fruit{ String name; String location; String recommend; Fruit(String _name,String _location,String _recommend){ name = _name; location = _location; recommend = _recommend; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setLocation(String location) { this.location = location; } public String getLocation() { return location; } public void setRecommend(String recommend) { this.recommend = recommend; } public String getRecommend() { return recommend; } } } |
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
1 2 3 4 5 |
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.2</version> </dependency> |
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