我正在尝试传递 JSON,但无法为其提供正确的格式,因此在读取时会出现错误。
Map<String, List<PojoClass>> mapObj = (Map<String, List<PojoClass>>) input.getData();
基本上从以下位置获取数据:
protected Map<String, List<Data>> data;
public Map<String, List<Data>> getdata() {
return data;
}
在 PojoClass 中,假设我们有一些变量,命名为姓名、地址、电话等。
错误:
> nested exception is
> com.fasterxml.jackson.databind.JsonMappingException: Can not
> deserialize instance of java.util.LinkedHashMap out of START_ARRAY
> token\n at [Source: java.io.PushbackInputStream@453fc83a; line: 7,
> column: 27] (through reference chain:
> co.age.ning.mon.dto.PojoClass[\"data\"])"
JSON:
"data": [
{
"key": [
{
"id": "id_1",
"time": "1",
"code": "xx",
"val": "ty",
"op": "false"
}
]
}
]
最佳答案
这可以通过 org.json.JSONObject "JSONObject.wrap([object])"来完成
import org.json.JSONObject;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Pojo {
public String name;
public String address;
public String contactNo;
JSONObject toJsonObject(){
return (JSONObject) JSONObject.wrap(this);
}
void convertMap(){
Map<String,List<Pojo>> map = new HashMap<>();
List<Pojo> list = new LinkedList<>();
list.add(new Pojo());
map.put("one",list);
for(Map.Entry entry : map.entrySet()){
for(Pojo expression : (List<Pojo>) entry.getValue()){
JSONObject object = expression.toJsonObject();
}
}
}
}
关于java - 无法为 map 设置正确的 JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41277871/