Perhaps you're looking for:
mapper.convertValue(jsonNode, MyPojo.class)
Javadoc: ObjectMapper.convertValue(Object, Class)
Convenience method for doing two-step conversion from given value, into instance of given value type, by writing value into temporary buffer and reading from the buffer into specified target type. This method is functionally similar to first serializing given value into JSON, and then binding JSON data into value of given type, but should be more efficient since full serialization does not (need to) occur. However, same converters (serializers, deserializers) will be used as for data binding, meaning same object mapper configuration works.
There's more to the Javadoc, but that's the gist of it.
Answer from dnault on Stack OverflowConvert javax.json.JsonObject to com.fasterxml.jackson.databind.JsonNode - Stack Overflow
Convert JSONNode to JSON - Unity Engine - Unity Discussions
json - Convert Jackson object into JSONObject java - Stack Overflow
json - Convert Java Object to JsonNode in Jackson - Stack Overflow
public JsonNode toJsonNode(JsonObject jsonObj) {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readTree(jsonObj.toString());
}
this will just to it. JsonObject.toString() will convert to json String, you don't need to use anything else.
The following solution parses a javax.json.JsonObject into a JSON string and then parses the JSON string into a com.fasterxml.jackson.databind.JsonNode using Jackson's ObjectMapper:
public JsonNode toJsonNode(JsonObject jsonObject) {
// Parse a JsonObject into a JSON string
StringWriter stringWriter = new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) {
jsonWriter.writeObject(jsonObject);
}
String json = stringWriter.toString();
// Parse a JSON string into a JsonNode
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);
return jsonNode;
}
The way you are doing is work fine, because i also use that way to make a JSONobject.
here is my code
Copy public JSONObject getRequestJson(AccountInquiryRequestVO accountInquiryRequestVO) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
JSONObject jsonAccountInquiry;
jsonAccountInquiry=new JSONObject(mapper.writeValueAsString(accountInquiryRequestVO));
return jsonAccountInquiry;
}
its working fine for me. but you can always use JsonNode also here is the sample code for that
CopyJsonNode jsonNode=mapper.valueToTree(accountInquiryRequestVO);
its very easy to use.
Right now, you are serializing your Pojo to a String, then parsing that String and converting it into a HashMap style object in the form of JSONObject.
This is very inefficient and doesn't accomplish anything of benefit.
Jackson already provides an ObjectNode class for interacting with your Pojo as a JSON object. So just convert your object to an ObjectNode. Here's a working example
Copypublic class Example {
public static void main(String[] args) throws Exception {
Pojo pojo = new Pojo();
pojo.setAge(42);
pojo.setName("Sotirios");
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.valueToTree(pojo);
System.out.println(node);
}
}
class Pojo {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Otherwise, the way you are doing it is fine.
A slight variation on Richards answer but readTree can take a string so you can simplify it to:
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree("{\"k1\":\"v1\"}");
You need to use an ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = mapper.getJsonFactory(); // since 2.1 use mapper.getFactory() instead
JsonParser jp = factory.createJsonParser("{\"k1\":\"v1\"}");
JsonNode actualObj = mapper.readTree(jp);
Further documentation about creating parsers can be found here.