SimpleJson is a library for writing or reading JSON easily.
You can use as similar to Map.
This library was made using Jackson.
- ver1.0.0: SimpleJson-1.0.0.jar
<repositories>
...
<repository>
<id>github</id>
<name>SimpleJson</name>
<url>https://raw.githubusercontent.com/Smile-NS/SimpleJson/mvn-repo/</url>
</repository>
...
</repositories>
<dependencies>
...
<dependency>
<groupId>io.github.smile-ns.simplejson</groupId>
<artifactId>SimpleJson</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>Here we show you how to use it and some sample code.
However, you will need to look at the JavaDoc, as not everything here is available.
SimpleJson json = new SimpleJson(); // An empty object.// An object from the json file.
// If the file already exists, SimpleJson creates a new file by the path.
SimpleJson json = new SimpleJson(file);SimpleJson json = new SimpleJson(new Foo()); // An object from the instance.SimpleJson json = new SimpleJson("{\"smile\":\"noob\"}"); // An object from the json text.Put a (pseudo) primitive type element.
SimpleJson json = new SimpleJson();
json.put("id", 10);
json.put("name", "test");
...{
"id": 10,
"name": "test"
}You can put a value in a deep node with delimiter characters.
If the node doesn't exist, SimpleJson creates an empty node.
SimpleJson json = new SimpleJson();
json.put("smile.iq", 80);{
"smile": {
"iq": 80
}
}Put a reference type element.
- Map and List
SimpleJson json = new SimpleJson();
Map<String, Integer> map = new HasMap<>();
map.put("key", 3);
json.put("map", map);
List<String> list = new ArrayList<>();
list.add("smile");
list.add("angry");
json.put("list", list);{
"map": {
"key": 3
},
"list": [ "smile", "angry" ]
}- instance
class Foo {
int id = 1;
String name = "test";
}
SimpleJson json = new SimpleJson();
json.put("Foo", new Foo());{
"Foo": {
"id": 1,
"name": "test"
}
}SimpleJson json = new SimpleJson();
SimpleJson json2 = new SimpleJson();
json2.put("key", 10);
json.put("json2", json2);{
"json2": {
"key": 10
}
}Remove a node.
You can operate as well as put.
json.remove(key, value);Get a node as (pseudo) primitive type, their wrapper classes, or reference type.
json.getInt("key");
json.getList("key2");Convert and get an object.
- SimpleJson#toString()
- Convert the object into String
- SimpleJson#toJsonNode()
- Convert the object into JsonNode
- SimpleJson#toJavaObject()
- Convert the object into an instance of the set class
- SimpleJson#toMap()
- Convert the object into Map
Save in a file.
You can't save if you haven't set the file to save from the constructor or SimpleJsonProperty#setFile(file).
json.save();