How to convert List to a JSON Object using GSON?

There is a sample from google gson documentation on how to actually convert the list to json string:

Type listType = new TypeToken<List<String>>() {}.getType();
 List<String> target = new LinkedList<String>();
 target.add("blah");

 Gson gson = new Gson();
 String json = gson.toJson(target, listType);
 List<String> target2 = gson.fromJson(json, listType);

You need to set the type of list in toJson method and pass the list object to convert it to json string or vice versa.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)