上案例:
一
public static UserData toBean(String JsonStr){
UserData data=null;
try {
data = new JSONDeserializer<UserData>().deserialize(JsonStr
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
二:
public static List<UserData> toListBean(String str){
List<UserData> list = null;
try {
list = new JSONDeserializer<List<UserData>>().use("userinfo",ArrayList.class).use("values", UserData.class).deserialize(str);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
三:
public class JSONInfo<T> {
private int errCode;
private String errMsg;
private List<T> userinfo;
private Long updateTime;
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public List<T> getUserinfo() {
return userinfo;
}
public void setUserinfo(List<T> userinfo) {
this.userinfo = userinfo;
}
}
public static JSONInfo<List<UserData>> toBean2(String JsonStr){
JSONInfo<List<UserData>> info = null;
try {
info = new JSONDeserializer<JSONInfo<List<UserData>>>()
.use("userinfo", ArrayList.class).use("userinfo.values", UserData.class)
.deserialize(JsonStr,JSONInfo.class);
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
但是这样用到的是反射的形式,有很大的局限性,也就是说,use("userinfo", ArrayList.class)这语句里面你的String是要和JSONInfo里的字段是一致的JSOInfo里面要有get和set方法。这样没写一个bean都要有一个对应的JSONInfo。于是有下面的
四:
//测试List的
public static void getMM(String json){
try {
int i =0;
System.out.println(i);
List<BerthData> list = null;
// Object obj = new JSONDeserializer().deserialize(json);
// Map map = (Map) new JSONDeserializer().deserialize(json);
// List list = (List) map.get("userinfo");
// String str = list.get(0).toString();
// List list = (List) new JSONDeserializer().deserialize(json,ArrayList.class);
// Object obj = list.get(0);
// System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
//测试类
public static void getDataBean(String jsonStr){
try {
BerthData userData = BerthData.gertBerthData(jsonStr);
System.out.println(userData);
} catch (Exception e) {
e.printStackTrace();
}
}
//测试map
public static void getMap(String jsonStr){
try {
Map map = BaseData.getMap(jsonStr);
System.out.println(map);
} catch (Exception e) {
e.printStackTrace();
}
}
flexjson是可以通过直接 new JSONDeserializer().deserialize(json) 这样来取的,返回的对象你直接定义,得到的是一个HashMap,你可以通过get(key)来获取到数据,获取嵌套模式时,通过get("userinfo")来获取到一个LIst集合,而list集合里面存储的是键值对,也是Map的形式可以获取,然后在吧获取到的值通过bean的set方法添加到具体的类里面,这样十分的灵活,如下面的类里面的方法:
public static List<UserData> getUserDataList(String json){
List<UserData> userDatas = new ArrayList<UserData>();
List list = (List) deserializer.deserialize(json,ArrayList.class);
for(int i=0;i<list.size();i++){
UserData userData = new UserData();
Map map = (Map) list.get(i);
userData.setId((Long.valueOf(map.get("id")+"")) );
userData.setCityCode((String) map.get("cityCode"));
userData.setUserCode((String) map.get("userCode"));
userData.setUserName((String) map.get("userName"));
userData.setUserType((String) map.get("userType"));
userData.setOperateType((int) map.get("operateType"));
userData.setOperateTime((Long.valueOf(map.get("operateTime")+"")) );
userDatas.add(userData);
System.out.println(userData);
}
return userDatas;
}
public static UserData gertUserData(String json){
UserData userData = new UserData();
Map map = (Map) deserializer.deserialize(json,Map.class);
userData.setId((Long.valueOf(map.get("id")+"")));
userData.setCityCode((String) map.get("cityCode"));
userData.setUserCode((String) map.get("userCode"));
userData.setUserName((String) map.get("userName"));
userData.setUserType((String) map.get("userType"));
userData.setOperateType((int) map.get("operateType"));
userData.setOperateTime((Long.valueOf(map.get("operateTime")+"")));
return userData;
}
public class BaseData {
protected static JSONDeserializer deserializer = new JSONDeserializer();
public static Map getMap(String json){
Map map = (Map) deserializer.deserialize(json,Map.class);
return map;
}
}