我使用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