My implementation of methods handling JSON data in Java. The parser and lexer used here were appropriated and translated from Eaton Phil's awesome pj library found at https://github.com/eatonphil/pj/blob/master/pj/lexer.py. Pull requests fixing anything are always appreciated, as is testing and the reporting of issues. This library is not bug free by any means yet. If you don't want to spend time dealing with some downright weird bugs, you might want to find another solution to your problem.
Grab a JAR from the release page or clone this project and insert it into your project. To make a JSON object that contains your information, create a new instance of the JSONObject class like this:
JSONObject json = new JSONObject();
json.addRule("name", "Mweya");
json.addRule("age", 20);
json.addRule("bored", true);
This even works for arrays!
int[] ages = {12, 23, 55};
json.addRule("ages", ages);
Documents and arrays of rules can be added in a similar fashion:
JSONObject location = newJSONObject();
location.addRule("lat", 31.445363);
location.addRule("long", 23.343567);
json.addRule("location", location);
JSONRule rule = json.searchSelector("name");
json.deleteRule("name");
json.removeRule("location");
Rules at a certain index can be deleted in a similar fashion:
json.deleteRule(2);
json.toFile("file.json");
This feature is currently very buggy and should probably not be used yet. As always, pull requests would be appreciated!
json.fromFile("path/to/file.json");
If you need to save a JSONObject, try using serialization for now.