Java FastJson 获取指定 List 并遍历 Map 获取 key 值
Java FastJson 获取指定 List 并遍历 Map 获取 key 值
本文将介绍如何使用 Java FastJson 库从 JsonObject 中获取名为 'weightList' 的 List,并遍历 List 中的 Map 获取 key 为 'orderId' 的字符串内容。
代码示例:
import com.alibaba.fastjson.JSONObject;
JSONObject jsonObject = new JSONObject(); // 假设已经有一个FastJson的JSONObject对象
List<Map<String, Object>> weightList = jsonObject.getObject('weightList', List.class); // 获取名称为weightList的List
for (Map<String, Object> map : weightList) { // 遍历weightList中的Map
String orderId = (String) map.get('orderId'); // 获取key为orderId的字符串
System.out.println(orderId); // 打印orderId
}
解释:
jsonObject.getObject('weightList', List.class)方法可以获取名称为 'weightList' 的 List。- 使用 for 循环遍历
weightList中的 Map。 map.get('orderId')方法获取 key 为 'orderId' 对应的值,并将其转换为字符串类型。- 打印
orderId。
注意:
- 假设
jsonObject对象已经包含名为 'weightList' 的 List。 - 假设 List 中的 Map 都包含 key 为 'orderId' 的字符串值。
希望本文能帮助您理解如何使用 Java FastJson 获取指定 List 并遍历 Map 获取 key 值。
原文地址: https://www.cveoy.top/t/topic/nJgK 著作权归作者所有。请勿转载和采集!