I'm trying to write json data to a json file.
After code execution no errors are thrown but the .json file is empty.
Please find below code and help on this
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
public class Test {
public static void main(String[] args) throws JSONException {
try {
List<String> foo = new ArrayList<String>();
foo.add("1");
foo.add("2");
foo.add("3");
System.out.println("values :: "+foo);
Writer writer = new FileWriter("operatorList.json");
Gson gson = new GsonBuilder().create();
gson.toJson(foo, writer);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
My previous answer didn't help you. Try this one. The issue seems to be with serialising/deserializing lists. I hope it helps, you should just be able to run that.