博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数组json格式的字符串 转 list<Bean>
阅读量:6155 次
发布时间:2019-06-21

本文共 3738 字,大约阅读时间需要 12 分钟。

1.  字符串形式:

[    {        "userid": "admin",        "name": "admin",        "password": "MTIzNDU2",        "flag": 1,        "createtime": 1467277229000,        "nickname": "",        "thumbnail": null,        "lastDate": null,        "status": null,        "region": "22222"    },    {        "userid": "administrator",        "name": "administrator",        "password": "MTIzNDU2",        "flag": 0,        "createtime": 1467252566000,        "nickname": "超级管理员用户",        "thumbnail": null,        "lastDate": null,        "status": null,        "region": ""    }]

2. javabean

import java.util.Date;public class User {    private String userid;    private String name;    private String password;    private Byte flag;    private Date createtime;    private String nickname;    private String thumbnail;    private Date lastDate;    private Byte status;    private String region;    public String getUserid() {        return userid;    }    public void setUserid(String userid) {        this.userid = userid == null ? null : userid.trim();    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name == null ? null : name.trim();    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password == null ? null : password.trim();    }    public Byte getFlag() {        return flag;    }    public void setFlag(Byte flag) {        this.flag = flag;    }    public Date getCreatetime() {        return createtime;    }    public void setCreatetime(Date createtime) {        this.createtime = createtime;    }    public String getNickname() {        return nickname;    }    public void setNickname(String nickname) {        this.nickname = nickname == null ? null : nickname.trim();    }    public String getThumbnail() {        return thumbnail;    }    public void setThumbnail(String thumbnail) {        this.thumbnail = thumbnail == null ? null : thumbnail.trim();    }    public Date getLastDate() {        return lastDate;    }    public void setLastDate(Date lastDate) {        this.lastDate = lastDate;    }    public Byte getStatus() {        return status;    }    public void setStatus(Byte status) {        this.status = status;    }    public String getRegion() {        return region;    }    public void setRegion(String region) {        this.region = region == null ? null : region.trim();    }}

 

3. 转化

     String listUsers = userClient.listUsers();        JSONArray jArray= JSONArray.fromObject(listUsers);        Collection collection = JSONArray.toCollection(jArray, User.class);        List
userList = new ArrayList
(); Iterator it = collection.iterator(); while (it.hasNext()) { User user = (User) it.next(); userList.add(user); }

 4. 补充(20181025)

part3 部分 之前没有用到com.alibaba.fastjson 相关API

fastjson对这种情况 做了封装处理

User.class

public class User{    private String name;    private Integer age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    @Override    public String toString() {        return "User{" +                "name='" + name + '\'' +                ", age=" + age +                '}';    }}
View Code

Main.class

private static  void method4(){        String msg = "[{\"name\":\"wang\",\"age\":15},{\"name\":\"li\",\"age\":18}]";        List
users = JSONArray.parseArray(msg, User.class); System.out.println(users); }

 

转载于:https://www.cnblogs.com/rocky-fang/p/5631164.html

你可能感兴趣的文章
ubuntu17.04 调试系统工具bcc,systamtap安装
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
查看>>
给初级拍摄者的十条好建议
查看>>
彻底搞清楚javascript中的require、import和export
查看>>
怎样一步步用D3画多曲线
查看>>
JAVA实现N皇后问题(回溯法)
查看>>
HDU 2689 Sort it【树状数组】
查看>>
周末,说声php的setter&getter(魔术)方法,你们辛苦了
查看>>
项目管理二三事
查看>>
SVN常用命令及在windows上安装SVN
查看>>
cocos2d-x 中文 乱码问题
查看>>
android 定时打电话教程
查看>>
Dll 导出类 [示例代码]
查看>>
在ListCtrl控件中设置自定义光标
查看>>
如何使java中double类型不以科学计数法表示
查看>>
session的使用
查看>>
shell for循环
查看>>
整型进制转换程序
查看>>
[Silverlight入门系列]使用MVVM模式(6):使用Behavior
查看>>
单例模式(Singleton)
查看>>