Skip to content

Instantly share code, notes, and snippets.

@CoderCouple
Forked from madonnelly/GSONFileTest.java
Created June 20, 2017 00:28
Show Gist options
  • Save CoderCouple/49be12cc4aaed983794b52b058ffe230 to your computer and use it in GitHub Desktop.
Save CoderCouple/49be12cc4aaed983794b52b058ffe230 to your computer and use it in GitHub Desktop.
Converting a file to JsonObject with GSON
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class GSONFileTest {
public static void main(String[] args)
{
convertFileToJSON ("/tmp/test.json");
}
public static JsonObject convertFileToJSON (String fileName){
// Read from File to String
JsonObject jsonObject = new JsonObject();
try {
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(new FileReader(fileName));
jsonObject = jsonElement.getAsJsonObject();
} catch (FileNotFoundException e) {
} catch (IOException ioe){
}
return jsonObject;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment