实现JavaBean和json之间的相互转换
来源:互联网 发布:带租约商铺 知乎 编辑:程序博客网 时间:2024/05/22 01:44
使用json-lib组件实现
* 需要
* json-lib-2.4-jdk15.jar
* ezmorph-1.0.6.jar
* commons-collections-3.1.jar
* commons-lang-2.0.jar
支持
UserInfos实体类
private String id;private String name;..get/set
LoginAccount实体类
private String id;private String account;private UserInfos userInfos;..get/set
javaBean转换为Json
//创建LoginAccount对象的集合 List<LoginAccount> listJson=new ArrayList<LoginAccount>(); //制作数据 LoginAccount lAccount1=new LoginAccount(); lAccount1.setId("L001"); lAccount1.setAccount("xiaoMing"); UserInfos userInfos1=new UserInfos(); userInfos1.setId("001"); userInfos1.setName("小明"); lAccount1.setUserInfos(userInfos1); LoginAccount lAccount2=new LoginAccount(); lAccount2.setId("L002"); lAccount2.setAccount("xiaoHong"); UserInfos userInfos2=new UserInfos(); userInfos2.setId("002"); userInfos2.setName("小红"); lAccount2.setUserInfos(userInfos2); LoginAccount lAccount3=new LoginAccount(); lAccount3.setId("L003"); lAccount3.setAccount("xiaoHei"); UserInfos userInfos3=new UserInfos(); userInfos3.setId("003"); userInfos3.setName("小黑"); lAccount3.setUserInfos(userInfos3); //添加数据 listJson.add(lAccount1); listJson.add(lAccount2); listJson.add(lAccount3); //将List转换为JSON JSONArray jsonArray=JSONArray.fromObject(listJson); //输出JSON字符串 System.out.println(jsonArray.toString());
json转换为javaBean
//创建JSON字符串String loginAccountJSON="[{id:\"L0001\",account:\"xiaoMing\",userInfos:{id:\"U0001\",name:\"小明\"}},{id:\"L0001\",account:\"xiaoHong\",userInfos:{id:\"U0002\",name:\"小红\"}},{id:\"L0001\",account:\"xiaoHei\",userInfos:{id:\"U0003\",name:\"小黑\"}}]";//创建JSON对象JSONArray jsonArrays = (JSONArray) JSONSerializer.toJSON(loginAccountJSON);//设置list集合List list=(List) JSONSerializer.toJava(jsonArrays);//遍历for(Object object:list){JSONObject jsonObject = JSONObject.fromObject(object);LoginAccount loginAccount = (LoginAccount) JSONObject.toBean(jsonObject,LoginAccount.class);//输出获得的LoginAccount实体类的数据System.out.println("id="+loginAccount.getId()+";account="+loginAccount.getAccount()+";userInfo.id="+loginAccount.getUserInfos().getId()+";userInfo.name="+loginAccount.getUserInfos().getName());}
- 实现JavaBean和json之间的相互转换
- Json、JavaBean、Xml之间的相互转换
- javaBean和xml之间的相互转换
- 使用 XStream实现JavaBean 与 XML/JSON 之间相互转换
- javaBean与xml、json之间的相互转换
- Json、JavaBean、Xml之间的相互转换工具
- javabean、xml、json、map之间的相互转换
- json和JavaBean,String之间的转换
- 实现实体类和json类之间的相互转换
- Json 和字符串之间的相互转换
- xstream:实现JavaBean与XML/JSON的相互转换
- xstream:实现JavaBean与XML/JSON的相互转换
- xstream:实现JavaBean与XML/JSON的相互转换
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- 使用FastJson在JSON字符串、JSON对象及JavaBean、Map之间的相互转换
- STL概述
- WPF学习之启动窗体设置
- ORACLE 触发器实现主键自动填充
- POJ 3259 Wormholes
- 黑马程序员—java—IO流
- 实现JavaBean和json之间的相互转换
- android自定义布局中的平滑移动
- Ubuntu 13.04 – Install Jetty 9
- poj 2226 Muddy Fields(二分图)
- Exception:线程间操作无效: 从不是创建控件的线程访问它 解决方案
- ProgressBar自定义漂亮样式样式
- 面对SEO越来越空泛的局面我们应该做何选择
- WebService服务的发布及多种客户端方式调用
- Android的Touch事件处理机制