You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从List<Map<String,String>>解析属性值时返回null的问题求助

问题分析与解决方案

你的问题出在对attributes数组中每个元素的结构理解有误:每个子对象的键是id和value,而不是直接把id字段的内容作为键。所以你遍历每个Map<String, String>时,调用get("active")自然找不到对应键,返回null。

解决方法1:遍历过程中匹配目标id

直接在循环里判断每个元素的id是否为你要找的属性名,再提取对应的value:

List<Map<String, String>> attributes = (List<Map<String, String>>) request.get("attributes");
String activeValue = null;
for (Map<String, String> attribute : attributes) {
    String attrId = attribute.get("id");
    if ("active".equals(attrId)) {
        activeValue = attribute.get("value");
        // 如果需要转成布尔值
        boolean active = Boolean.parseBoolean(activeValue);
        break;
    }
}

解决方法2:先转换为以id为键的Map(推荐)

如果需要多次获取不同属性的值,建议先把整个attributes列表转换成一个以id为键、value为值的Map,后续操作会更便捷:

List<Map<String, String>> attributes = (List<Map<String, String>>) request.get("attributes");
Map<String, String> attrMap = new HashMap<>();
for (Map<String, String> attribute : attributes) {
    attrMap.put(attribute.get("id"), attribute.get("value"));
}

// 之后直接通过id获取值
String activeValue = attrMap.get("active");
String criticality = attrMap.get("Criticality");
// 按需转换类型
boolean active = Boolean.parseBoolean(activeValue);

关键提醒

注意id字段的大小写,比如示例中的Criticality首字母大写,匹配时要保持一致,避免因大小写不匹配导致找不到值。

内容的提问来源于stack exchange,提问作者Lins Pins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 22:10:35