Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List<String> to Json String

Tags:

java

json

How can I convert a List to a Json String?

I have managed to do it the other way round, but not this way.

I also don't know how I can specify the names of the keys then..

like image 272
Jane Doe Avatar asked Jul 04 '26 17:07

Jane Doe


1 Answers

You can use the Gson Library.

List<String> list;
String json = new Gson().toJson(list);

Edited:

Just to have the complete answer here: The problem is that you are converting the json String into a List<String>. This way you are losing the relation key-value. The correct should be convert the json string into a HashMap<>.

like image 184
Renato Lochetti Avatar answered Jul 07 '26 06:07

Renato Lochetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!