Reputation: 195
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();
}
}
}
Upvotes: 10
Views: 13942
Reputation: 619
You are in the right way, just flush() and close() the writer, like 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);
writer.flush(); //flush data to file <---
writer.close(); //close write <---
}catch(Exception e){
e.printStackTrace();
}
}
Upvotes: 43
Reputation: 1397
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.
public class Main {
public static void main(String[] args) {
List<String> operators = new ArrayList<>();
operators.add("Bruce Lee");
operators.add("Jackie Chan");
operators.add("Chuck Norris");
Type listOfStringObjects = new TypeToken<List<String>>(){}.getType();
Gson gson = new GsonBuilder().create();
String json = gson.toJson(operators, listOfStringObjects);
try(FileWriter writer = new FileWriter("operatorList.json")) {
writer.append(json);
System.out.println("Successfully serialized operators!");
}catch (IOException ex) {
System.err.format("An IO Exception was occurred: %s%n", ex);
System.exit(-1);
}
// Deserializing
System.out.println("Deserializing operators from JSON (Reading back)...");
try(BufferedReader reader = new BufferedReader(new FileReader("operatorList.json"))) {
StringBuilder jsonData = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
jsonData.append(line);
}
List<String> operatorsRead = gson.fromJson(jsonData.toString(), listOfStringObjects);
for(String operator : operatorsRead) {
System.out.println("Operator: " + operator);
}
}catch (Exception ex) {
System.err.format("An IO Exception was occurred: %s%n", ex);
}
}
}
Upvotes: 0