English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to use Jackson to search for values in a JSON file in Java?

com.fasterxml.jackson.databind.node.ObjectNodeThe class can be used to map JSON content to a JSON object structure. We can useObjectNode Classget()The method searches for a specific value in the JSON file, used to access the value of the specified field in the object node.

Syntax

public JsonNode get(String fieldName)

Example

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class ObjectNodeTest {
   public static void main(String args[]) throws Exception {
      String jsonString = "{\"Id\":101, "name": "Raja Ramesh", "address": "Madhapur"
      ObjectMapper mapper = new ObjectMapper();
      ObjectNode node = mapper.readValue(jsonString, ObjectNode.class);
      if(node.has("name")) {
         System.out.println("NAME: " + node.get("name"));
      }
   }
}

Output Result

NAME: "Raja Ramesh"