JAVA中json对象转JAVA对象,JSON数组(JSONArray)转集合(List)

 

json格式

{userId:'1',message:'2',create_time:'2020-03-28 20:58:11',create_date:'2020-03-28'}


JAVA对象
ChatDTO.java
public class ChatDTO {

    private String userId;

    private String message;

    private String createDate;

    private String createTime;

}

......
get set 方法

  

调用方法

JSONObject object = JSONObject.fromObject(json); //先转成json对象
ChatDTO chatDTO = (ChatDTO) JSONObject.toBean(object, ChatDTO.class);

  

 

 JSON数组转集合(JSONArray转list)

List list = (List) JSONArray.toCollection(jsonArray);

  

 

集合(List)转JSON数组

JSONArray array=JSONArray.fromObject(list)

  



 

posted @ 2020-03-28 23:03  yvioo  阅读(17715)  评论(0编辑  收藏  举报