Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JsonNode into Object

Tags:

I've got a JsonNode that is provided by an external library. I need to convert this JsonNode into it's POJO representation.

I've seen methods like this:

mapper.readValue(jsonNode.traverse(), MyPojo.class);

But I'm not very happy with this sollution. traverse() will actually convert my JsonNode into a String representation before it is deserialized into a POJO. The performance is an issue for me in this case.

Any other way of doing it?

Thanks

like image 987
pmartin8 Avatar asked Jul 29 '14 21:07

pmartin8


People also ask

What is difference between JsonNode and JSON object?

ObjectNode is a concrete implementation of JsonNode that maps a JSON object, and a JSON object is defined as following: An object is an unordered set of name/value pairs.

What is the difference between JsonNode and ObjectNode?

JsonNode represents any valid Json structure whereas ObjectNode and ArrayNode are particular implementations for objects (aka maps) and arrays, respectively.

How do you declare JsonNode?

Write JsonNode to JSONObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = readJsonIntoJsonNode(); String json = objectMapper. writeValueAsString(jsonNode); The readJsonIntoJsonNode() method is just a method I have created which parses a JSON string into a JsonNode - just so we have a JsonNode to write.

How do I read a JSON file using ObjectMapper?

Read Object From JSON via URL ObjectMapper objectMapper = new ObjectMapper(); URL url = new URL("file:data/car. json"); Car car = objectMapper. readValue(url, Car. class);


1 Answers

Perhaps you're looking for:

mapper.convertValue(jsonNode, MyPojo.class)
like image 160
dnault Avatar answered Sep 29 '22 05:09

dnault