1.json串转对象-JSONObject.parseObject
R r= platformApi.getUserinfo();
Userinfo user = JSONObject.parseObject(JSONObject.toJSONString(r.getData()),Userinfo.class);
R如下:
public class R<T> {
private String code;
private boolean success;
private String errorMsg;
private T data;
get、set方法省略。
}
r.getData()不是json串,需要通过JSONObject.toJSONString()转化为json串。
2.json串转对象列表-JSONObject.parseArray
R menusR = platformApi.getEnterpriseMenuList(enterpriseMenu);
List<EnterpriseMenu> menus = JSONObject.parseArray(JSONObject.toJSONString(menusR.getData()),EnterpriseMenu.class);
注意:
JSONObject导包为import com.alibaba.fastjson.JSONObject;
原文:https://www.cnblogs.com/nuomm/p/14931246.html