使用 Fastjson 获取 JsonObject 中 Map 的 key 值

假设我们已经有了一个名为 jsonObject 的 Fastjson JsonObject 对象,可以按照以下方式获取名称为 'content' 的 Map,并从中获取 key 为 'orderId' 的字符串:

// 获取名称为 'content' 的 Map
Map<String, Object> contentMap = jsonObject.getJSONObject('content').getInnerMap();

// 获取 key 为 'orderId' 的字符串
String orderId = (String) contentMap.get('orderId');

需要注意的是,由于 Fastjson JsonObject 的 getInnerMap() 方法返回的是一个 Map<String, Object>,因此获取到的 orderId 也需要进行类型转换。如果 'content' 中不存在 key 为 'orderId' 的值,contentMap.get('orderId') 将返回 null

Java Fastjson 获取 JsonObject 中 Map 的 key 值

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

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