The cn.hutool.json.JSONObject#getByPath(java.lang.String, java.lang.Class<T>) method is part of the Hutool library, which is a popular utility library in the Java ecosystem. This specific method is used to retrieve a value from a JSON object based on a specified path and convert it to a specified type.

Hutool JSON Library Overview

Hutool's JSON library provides an easy-to-use API for parsing, creating, and manipulating JSON objects and arrays. It is similar to other JSON libraries like Jackson and Gson but aims to be lightweight and feature-rich.

Method Signature

The method signature for getByPath is as follows:


j

public <T> T getByPath(String expression, Class<T> resultType)

Parameters

  • expression: A string representing the path to the desired value within the JSON object. This path is similar to JSONPath expressions and can navigate through nested objects and arrays.
  • resultType: The class type to which the retrieved value should be converted.

Return Value

The method returns an instance of type T, which is the value at the specified path converted to the specified type.

Usage Example

Here’s an example of how to use the getByPath method in practice:

  1. Include Hutool Library: Make sure to include the Hutool library in your project. If you are using Maven, add the following dependency to your pom.xml:

    
    
    xml

    <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.10</version> <!-- Use the latest version available --> </dependency>

  2. Create a JSONObject and Use getByPath:

    
    
    java

    import cn.hutool.json.JSONObject;
    import cn.hutool.json.JSONUtil;
    
    public class Example {
        public static void main(String[] args) {
            String jsonString = "{\"user\": {\"name\": \"John\", \"age\": 30, \"address\": {\"city\": \"New York\"}}}";
            JSONObject jsonObject = JSONUtil.parseObj(jsonString);
    
            // Retrieve the user's name
            String name = jsonObject.getByPath("user.name", String.class);
            System.out.println("Name: " + name); // Output: Name: John
    
            // Retrieve the user's age
            Integer age = jsonObject.getByPath("user.age", Integer.class);
            System.out.println("Age: " + age); // Output: Age: 30
    
            // Retrieve the user's city
            String city = jsonObject.getByPath("user.address.city", String.class);
            System.out.println("City: " + city); // Output: City: New York
        }
    }
    

Explanation

  1. Parsing JSON String: The JSONUtil.parseObj method is used to parse a JSON string into a JSONObject.
  2. Retrieving Values by Path:
    • getByPath("user.name", String.class) retrieves the value at the path "user.name" and converts it to a String.
    • getByPath("user.age", Integer.class) retrieves the value at the path "user.age" and converts it to an Integer.
    • getByPath("user.address.city", String.class) retrieves the value at the path "user.address.city" and converts it to a String.

Summary

The cn.hutool.json.JSONObject#getByPath method is a convenient way to navigate and extract values from a JSON object using a specified path. It supports type conversion, making it easy to retrieve and work with JSON data in a type-safe manner. This method is particularly useful when dealing with nested JSON structures.

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐