Using 'convertValue' method of ObjectMapper class, you can convert jsonNode to a map.
Example
Map<String, Object> result = mapper.convertValue(jsonNode, new TypeReference<Map<String, Object>>() {});
App.java
package com.sample.app;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
public static void main(String args[]) throws JsonProcessingException, IOException {
String json = "{\n" + " \"firstName\" : \"Krishna\",\n" + " \"lastName\" : \"Gurram\"\n" + "}";
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
Map<String, Object> result = mapper.convertValue(jsonNode, new TypeReference<Map<String, Object>>() {
});
for (String key : result.keySet()) {
System.out.println(key + " : " + result.get(key));
}
}
}
Output
firstName : Krishna
lastName : Gurram
No comments:
Post a Comment