快速JSON(FastJson)可以通过将Map对象转换为JSON字符串来获取Map的字符串表示形式。以下是一个示例代码:

import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;

public class MapToStringExample {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Alice");
        map.put("age", 20);
        map.put("gender", "female");
        
        String mapString = JSON.toJSONString(map);
        System.out.println(mapString);
    }
}

输出结果:

{"gender":"female","name":"Alice","age":20}

在上面的代码中,我们创建了一个包含三个键值对的Map对象,然后使用FastJson的JSON.toJSONString()方法将其转换为JSON字符串。最后,我们打印JSON字符串

fastjson获取Map的String

原文地址: https://www.cveoy.top/t/topic/dx9Q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录