@ConfigurationProperties填充地图<字符串,对象>



我使用Map<String, Object> data使动态

@Configuration
@ConfigurationProperties("test")
@Data
public class Config {
private Map<String, Object> data;
}

如何获得预期的数据?

例子yaml

test:
a: a
b: b
c: 
- name: name1
- name: name2

结果

{
"a": "a",
"b": "b",
"c": {
0: {
"name": "name1"
},
1: {
"name": "name2"
}
}
}

预计

{
"a": "a",
"b": "b",
"c": [
{
"name": "name1"
},
{
"name": "name2"
}
]
}

这是因为数据存储在映射中。将此数据添加到列表中,然后打印该列表以获得所需格式的数据。这样的

List<Map<String, Object>> cList = new ArrayList<>();
Map<Integer, Map<String, Object>> cMap = (Map<Integer, Map<String, Object>>) data.get("c");
for (Map<String, Object> value : cMap.values()) {
cList.add(value);
}
data.put("c", cList);
//Now your data is in list cList

相关内容

  • 没有找到相关文章

最新更新