poornerd

my thoughts on programming and other nerdy stuff

Examples for working with JSON in Play Framework 2 with Java

| 0 comments

  • String to JSON: 
    JsonNode json = mapper.readTree(notification.getSharedData());
  • Json to Object: 
    Asset asset = mapper.readValue(a.toString(), Asset.class)
  • Json to extisting Object: 
    asset = mapper.readerForUpdating(asset).readValue(assetJson.toString())
  • Object to JsonString:
    mapper.writeValueAsString(new ResultDocument(document))
  • Convert Object ot JsonNode:
    JsonNode documentNode = mapper.convertValue(document, JsonNode.class);

    This causes an error – Caused by: com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle so do this workaround:

    String messageJson = null;
    
    String documentAsString = BaseApiController.mapper.writeValueAsString(document);
    
    JsonNode documentNode = BaseApiController.mapper.readTree(documentAsString);
If you have read this far, you may as well follow me on Twitter:

Author: poornerd

Tech­nol­o­gist, Entre­pre­neur, Vision­ary, Pro­gram­mer :: Grad­u­ated from USC (Uni­ver­sity of South­ern Cal­i­for­nia) with a degree in Com­puter Sci­ence. After 10+ years of free­lance con­sult­ing and pro­gram­ming, he co-founded Site­Force AG eBusi­ness Solu­tions in 1999 in Munich (München), Ger­many.

Leave a Reply

Required fields are marked *.