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); ListuserList = 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 + '}'; }}
Main.class
private static void method4(){ String msg = "[{\"name\":\"wang\",\"age\":15},{\"name\":\"li\",\"age\":18}]"; Listusers = JSONArray.parseArray(msg, User.class); System.out.println(users); }