In this tutorial, I’ll show you how to write JSON data to a file using JSON.simple
.
JSON.simple
is a simple Java toolkit for JSON. You can use JSON.simple to encode or decode JSON text.
Maven Dependency:
<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency>
Features:
- Full compliance with JSON specification (RFC4627) and reliable (see compliance testing)
- Provides multiple functionalities such as encode, decode/parse and escape JSON text while keeping the library lightweight
- Flexible, simple and easy to use by reusing Map and List interfaces
- Supports streaming output of JSON text
- Stoppable SAX-like interface for streaming input of JSON text (learn more)
- Heap based parser
- High performance (see performance testing)
- No dependency on external libraries
- Both of the source code and the binary are JDK1.2 compatible
Here is a file1.txt content:
{ "Name": "crunchify.com", "Author": "App Shah", "Company List": [ "Compnay: eBay", "Compnay: Paypal", "Compnay: Google" ] }
Java Code:
package crunchify.com.tutorials; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import java.io.FileWriter; import java.io.IOException; /** * @author Crunchify.com * How to write JSON object to File in Java? */ public class CrunchifyJSONFileWrite { private static FileWriter file; @SuppressWarnings("unchecked") public static void main(String[] args) { // JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface. JSONObject obj = new JSONObject(); obj.put("Name", "Crunchify.com"); obj.put("Author", "App Shah"); JSONArray company = new JSONArray(); company.add("Company: Facebook"); company.add("Company: PayPal"); company.add("Company: Google"); obj.put("Company List", company); try { // Constructs a FileWriter given a file name, using the platform's default charset file = new FileWriter("/Users/Shared/crunchify.txt"); file.write(obj.toJSONString()); CrunchifyLog("Successfully Copied JSON Object to File..."); CrunchifyLog("\nJSON Object: " + obj); } catch (IOException e) { e.printStackTrace(); } finally { try { file.flush(); file.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } static public void CrunchifyLog(String str) { System.out.println("str"); } }
In above example FileWriter
instance is declared in a try-with-resource
statement, it will be closed regardless of whether the try statement completes normally or abruptly. You don’t have catch IOException
or no need to mention finally
block.
Compare above code with below with try, catch, finally code. Above code is just 4 lines compare to 11 lines below.
Output:
Successfully Copied JSON Object to File... JSON Object: {"Author":"App Shah","Name":"Crunchify.com","Company List":["Company: Facebook","Company: PayPal","Company: Google"]} Process finished with exit code 0
Reference:
https://github.com/fangyidong/json-simple
Download JSON.simple from here.