Skip to content

Instantly share code, notes, and snippets.

@mcSw4p
Last active August 22, 2017 03:49
Show Gist options
  • Save mcSw4p/ab37318dbd394f4b5cc5de5d6d3f6ab3 to your computer and use it in GitHub Desktop.
Save mcSw4p/ab37318dbd394f4b5cc5de5d6d3f6ab3 to your computer and use it in GitHub Desktop.
JSON Simple guide

JSON Simple | How to read and write JSON files

Download

[maven | .jar(direct) | .jar]

Read from file

// JSON Simple imports
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
File jsonFile = new File("/hello.json"); // Do with as you please

/*
* Simple key value pairs
*/
JSONParser parser = new JSONParser();
try{
	JSONObject jObj = (JSONObject) parser.parse(new FileReader(jsonFile));
} catch (IOException | ParseException e) {
	e.printStackTrace();
}

String value = (String) jObj.get("world");
long delay = (Long) jObj.get("delay");
boolean debug = (Boolean) jObj.get("debug");

/*
 * JSONArray
 */
JSONArray array = (JSONArray) jObj.get("array");

/*
* JSONObject inside JSONObject
*/
try{
	JSONObject user = (JSONObject) parser.parse(jObj.get("user").toString());
} catch (IOException | ParseException e) {
	e.printStackTrace();
}
String username = (String) user.get("username");

Write to file

// JSON Simple imports
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
File jsonFile = new File("/hello.json"); // Do with as you please

/*
 * Simple key value pairs
 */
JSONObject jObj = new JSONObject();
jObj.put("world", "Sw4p"); // String
jObj.put("delay", 600); // Long/Integer
jObj.put("debug", true); // Boolean

/*
 * JSONArray
 */
JSONArray array = new JSONArray(); // List/Array
array.add("value1");
array.add("value2");
jObj.put("array", array);

/*
 * JSONObject inside JSONObject
 */
JSONObject user = new JSONObject();
user.put("username", "Sw4p");
jObj.put("user", user);

// Write 
try (FileWriter fileWriter = new FileWriter(jsonFile)) {
	fileWriter.write(jObj.toJSONString());
	fileWriter.flush();
} catch (IOException e) {
	e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment