This wrapper simplifies the usage of popular JSON library Moshi by making adding collections for handling arbitrary structures.
Java
JSONObject obj = new JSONObject();
obj.put("hello", "world");
obj.put("example", 123);
String str = obj.toString();
System.out.println(str); // { "hello": "world", "example": 123 }
obj = new JSONObject(str);
System.out.println(str); // { "hello": "world", "example": 123 }
System.out.println(obj.getString("hello")); // world
obj.remove("hello");
System.out.println(str); // { "example": 123 }
JSONArray arr = new JSONArray();
arr.add(123);
arr.add("hello");
arr.add("world");
String str = obj.toString();
System.out.println(str); // [ 123, "hello", "world" ]
arr = new JSONArray(str);
System.out.println(arr); // [ 123, "hello", "world" ]
System.out.println(arr.getString(1)); // hello
arr.remove("hello");
System.out.println(arr); // [ 123, "world" ]
Groovy
def obj = new JSONObject()
obj.hello = "world"
obj.example = 123
String str = obj.toString()
println str // { "hello": "world", "example": 123 }
obj = new JSONObject(str)
println str // { "hello": "world", "example": 123 }
println str.getString("hello") // world
obj >> "hello"
println str // { "example": 123 }
import com.ygimenez.json.JSONArray
def arr = new JSONArray()
arr << 123
arr << "hello"
arr << "world"
String str = obj.toString()
println str // [ 123, "hello", "world" ]
arr = new JSONArray(str)
println arr // [ 123, "hello", "world" ]
println arr.getString(1) // hello
arr >> "hello"
println arr // [ 123, "world" ]
This library is available for manual installation and through Maven Central:
- Click on the releases tab on the top of this repository
- Download the latest release
- Put the .jar file somewhere in your project
- Add it to the buildpath
- Done!
- Add this library as a dependency:
Gradle:
dependencies {
implementation group: 'com.github.ygimenez', name: 'Simple-Moshi', version: 'VERSION'
}
Maven:
<dependency>
<groupId>com.github.ygimenez</groupId>
<artifactId>Simple-Moshi</artifactId>
<version>VERSION</version>
</dependency>
If you have any issues using this library feel free to create a new issue, I'll review it as soon as possible!