fastjson 转java bean中的map字段无效

f2uvfpb9  于 2021-11-27  发布在  Java
关注(0)|答案(3)|浏览(291)

比如这么一段json

{
"key":"client_bind",
"data":{
"channel":"browser",
"version":"1.0.0",
"osVersion":"1.0.0",
"packageName":"com.aaa.webapp",
"deviceId":"u",
"device":"Chrome"
}
}

对应的java对象是

public class Body  {
	private String key;
	private HashMap<String, String> data = new HashMap<String, String>();
}

通过Body body = JSON.parseObject(json, Body.class);解析后 data是空的

dfddblmv

dfddblmv1#

你的 定义 channel version osVersion ... 这些属性吧
private entity data = new entity();

b0zn9rqh

b0zn9rqh2#

@Funset
加上get/set方法就可以了

kx7yvsdv

kx7yvsdv3#

三种解决办法:

  1. 如 @Fan4j 说的添加getter/setter
  2. 修改field的private为public
    修改为
public class Body  {
	public String key;
	public HashMap<String, String> data = new HashMap<String, String>();
}
  1. 配置FieldBased https://github.com/alibaba/fastjson/wiki/FieldBased_cn

相关问题