Play documentation: https://www.playframework.com/documentation/2.5.x/JavaJsonActions
import play.libs.Json;
public JsonNode createJson() {
// {"id": 33, "values": [3, 4, 5]}
ObjectNode rootNode = Json.newObject();
ArrayNode listNode = Json.newArray();
long values[] = {3, 4, 5};
for (long val: values) {
listNode.add(val);
}
rootNode.put("id", 33);
rootNode.set("values", listNode);
return rootNode;
}
import play.libs.Json;
// (...)
// Note: "app" is an play.Application instance
JsonNode node = Json.parse(app.resourceAsStream("public/myjson.json"));
String myStr = "{\"name\": \"John Doe\"}";
JsonNode node = Json.parse(myStr);
In the following examples, json
contains a JSON object with the following data:
[
{
"name": "John Doe",
"work": {
"company": {
"name": "ASDF INC",
"country": "USA"
},
"cargo": "Programmer"
},
"tags": ["java", "jvm", "play"]
},
{
"name": "Bob Doe",
"work": {
"company": {
"name": "NOPE INC",
"country": "AUSTRALIA"
},
"cargo": "SysAdmin"
},
"tags": ["puppet", "ssh", "networking"],
"active": true
}
]
JsonNode node = json.get(0).get("name"); // --> "John Doe"
// This will throw a NullPointerException, because there is only two elements
JsonNode node = json.get(2).get("name"); // --> *crash*
JsonNode node1 = json.at("/0/name"); // --> TextNode("John Doe")
JsonNode node2 = json.at("/2/name"); // --> MissingNode instance
if (! node2.isMissingNode()) {
String name = node2.asText();
}
JsonNode node2 = json.at("/0/work/company/country"); // TextNode("USA")
List<JsonNode> d = json.findValues("country"); // List(TextNode("USA"), TextNode("AUSTRALIA"))
List<JsonNode> e = json.findParents("active"); // List(ObjectNode("Bob Doe"))
By default, Jackson (the library Play JSON uses) will try to map every public field to a json field with the same name. If the object has getters/setters, it will infer the name from them. So, if you have a Book
class with a private field to store the ISBN and have get/set methods named getISBN/setISBN
, Jackson will
setISBN
method to define the isbn field in the Java object (if the JSON object has a "ISBN" field).public class Person {
String id, name;
}
JsonNode node = Json.parse("{\"id\": \"3S2F\", \"name\", \"Salem\"}");
Person person = Json.fromJson(node, Person.class);
System.out.println("Hi " + person.name); // Hi Salem
// "person" is the object from the previous example
JsonNode personNode = Json.toJson(person)
// personNode comes from the previous example
String json = personNode.toString();
// or
String json = Json.stringify(json);
System.out.println(personNode.toString());
/* Prints:
{"id":"3S2F","name":"Salem"}
*/
System.out.println(Json.prettyPrint(personNode));
/* Prints:
{
"id" : "3S2F",
"name" : "Salem"
}
*/