【发布时间】:2021-05-25 20:30:24
【问题描述】:
我在 JAVA 中有这个:private Map<String,String> car;
然后地图的其他类将被转换为这样的地图列表
List<Map<String,String>> car;
and then here is the implementation
List<Map<String, String>> mylist = new ArrayList<>();
CarEvidence.getCarFieldsEvidence().forEach((key, value) -> {
mylist.add(new HashMap<String,String>(){{
put("field",key);
put("value",value);
}});
});
如何将其转换为 JSON 对象?如果我将此发送到服务会引发错误,无效的 json。
......“其他 JSON 参数”......
"mylist": [
{
"field": "unique",
"secondValue": "pontiac",
"value": "10000"
},
{
"field": "other",
"secondValue": null,
"value": "any value"
},
{
"field": "log",
"secondValue": null,
"value": "contract"
},
{
"field": "message",
"secondValue": null,
"value": "last value"
}
]
但是如果我发送这个服务返回 200 ok
"mylist":
{
"field": "firstValue",
"secondValue": null,
"value": ""
}
请帮忙:(
【问题讨论】:
-
嗯,显然,端点不希望
myList是一个列表,而是一个对象。 -
在那种情况下使用
HashMap是完全没有意义的;只需使用ArrayList<SomeModel>。