博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
json转成实体bean,并解析出多层次属性信息
阅读量:7009 次
发布时间:2019-06-28

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

hot3.png

目的:将json转成实体bean,并解析出多层次属性信息此为json串:String json = "{"jsonValue":[{"addTime":1431078503000,"attriDetail":"[{\"group\":11202,\"name\":\"基本参数\",\"sort\":1,\"attribute\":[{\"id\":200511,\"name\":\"项目区域要求\",\"unit\":\"\",\"typeid\":2,\"is_full\":true,\"sort\":1,\"detail\":[{\"id\":13004689,\"detail\":\"北京\",\"sort\":1,\"checked\":\"\"},{\"id\":13004690,\"detail\":\"天津\",\"sort\":2,\"checked\":\"\"},{\"id\":13004691,\"detail\":\"河北\",\"sort\":3,\"checked\":\"\"},{\"id\":13004692,\"detail\":\"山西\",\"sort\":4,\"checked\":\"\"},{\"id\":13004693,\"detail\":\"内蒙古\",\"sort\":5,\"checked\":\"checked\"},{\"id\":13004694,\"detail\":\"辽宁\",\"sort\":6,\"checked\":\"\"},{\"id\":13004695,\"detail\":\"吉林\",\"sort\":7,\"checked\":\"\"},{\"id\":13004696,\"detail\":\"黑龙江\",\"sort\":8,\"checked\":\"\"},{\"id\":13004697,\"detail\":\"江苏\",\"sort\":9,\"checked\":\"\"},{\"id\":13004698,\"detail\":\"上海\",\"sort\":10,\"checked\":\"\"},{\"id\":13004699,\"detail\":\"浙江\",\"sort\":11,\"checked\":\"\"},{\"id\":13004700,\"detail\":\"安徽\",\"sort\":12,\"checked\":\"checked\"},{\"id\":13004701,\"detail\":\"福建\",\"sort\":13,\"checked\":\"\"},{\"id\":13004702,\"detail\":\"江西\",\"sort\":14,\"checked\":\"\"},{\"id\":13004703,\"detail\":\"山东\",\"sort\":15,\"checked\":\"\"},{\"id\":13004704,\"detail\":\"河南\",\"sort\":16,\"checked\":\"\"},{\"id\":13004705,\"detail\":\"湖北\",\"sort\":17,\"checked\":\"\"},{\"id\":13004706,\"detail\":\"湖南\",\"sort\":18,\"checked\":\"checked\"},{\"id\":13004707,\"detail\":\"广东\",\"sort\":19,\"checked\":\"checked\"},{\"id\":13004708,\"detail\":\"广西\",\"sort\":20,\"checked\":\"\"},{\"id\":13004709,\"detail\":\"海南\",\"sort\":21,\"checked\":\"\"},{\"id\":13004710,\"detail\":\"重庆\",\"sort\":22,\"checked\":\"\"},{\"id\":13004711,\"detail\":\"四川\",\"sort\":23,\"checked\":\"\"},{\"id\":13004712,\"detail\":\"贵州\",\"sort\":24,\"checked\":\"\"},{\"id\":13004713,\"detail\":\"云南\",\"sort\":25,\"checked\":\"\"},{\"id\":13004714,\"detail\":\"西藏\",\"sort\":26,\"checked\":\"\"},{\"id\":13004715,\"detail\":\"陕西\",\"sort\":27,\"checked\":\"\"},{\"id\":13004716,\"detail\":\"甘肃\",\"sort\":28,\"checked\":\"\"},{\"id\":13004717,\"detail\":\"青海\",\"sort\":29,\"checked\":\"\"},{\"id\":13004718,\"detail\":\"宁夏\",\"sort\":30,\"checked\":\"\"},{\"id\":13004719,\"detail\":\"新疆\",\"sort\":31,\"checked\":\"\"},{\"id\":13004720,\"detail\":\"香港\",\"sort\":32,\"checked\":\"checked\"},{\"id\":13004721,\"detail\":\"澳门\",\"sort\":33,\"checked\":\"\"},{\"id\":13004722,\"detail\":\"台湾\",\"sort\":34,\"checked\":\"\"}],\"userdata\":\"\"},{\"id\":200512,\"name\":\"加盟金额 \",\"unit\":\"\",\"typeid\":1,\"is_full\":true,\"sort\":2,\"detail\":[{\"id\":13004723,\"detail\":\"1万以下\",\"sort\":1,\"checked\":\"\"},{\"id\":13004724,\"detail\":\"1—3万\",\"sort\":2,\"checked\":\"\"},{\"id\":13004725,\"detail\":\"3—10万\",\"sort\":3,\"checked\":\"checked\"},{\"id\":13004726,\"detail\":\"10—30万\",\"sort\":4,\"checked\":\"\"},{\"id\":13004727,\"detail\":\"30万以上\",\"sort\":5,\"checked\":\"\"}],\"userdata\":\"\"},{\"id\":0,\"name\":\"测试1\",\"unit\":\"\",\"typeid\":0,\"is_full\":false,\"sort\":2,\"detail\":[],\"userdata\":\"测试2\"},{\"id\":0,\"name\":\"你好\",\"unit\":\"\",\"typeid\":0,\"is_full\":false,\"sort\":2,\"detail\":[],\"userdata\":\"哈哈\"},{\"id\":0,\"name\":\"公牛\",\"unit\":\"\",\"typeid\":0,\"is_full\":false,\"sort\":2,\"detail\":[],\"userdata\":\"骑士\"}]}]","categoryCode":"139102103","id":73761923,"memberId":41287423,"name":"asdasdasdasd","pic":"[43249444]","siteId":1,"summary":"asdasd"}],"status":1}  " ;解决方法1:一:首先 建立 4 个实体类对象(因为json串中包含有四个实体类信息)   1.import java.io.Serializable;import java.util.ArrayList;import java.util.List; /** * @author z** * */public class Demo1   implements Serializable{private static final long serialVersionUID = 1L; private Long id; private int siteId; private String memberId; private String categoryCode; private String addTime; private String pic; private String summary; private String name; private String attriDetail; private List
 attriDetails;   .....setter and getter    @Override public int hashCode() {  final int prime = 31;  int result = 1;  result = prime * result    + ((attriDetail == null) ? 0 : attriDetail.hashCode());  result = prime * result    + ((categoryCode == null) ? 0 : categoryCode.hashCode());  result = prime * result + ((id == null) ? 0 : id.hashCode());  result = prime * result    + ((memberId == null) ? 0 : memberId.hashCode());  result = prime * result + ((name == null) ? 0 : name.hashCode());  result = prime * result + ((pic == null) ? 0 : pic.hashCode());  result = prime * result + siteId;  result = prime * result + ((summary == null) ? 0 : summary.hashCode());  return result; } @Override public boolean equals(Object obj) {  if (this == obj)   return true;  if (obj == null)   return false;  if (getClass() != obj.getClass())   return false;  Information other = (Information) obj;  if (attriDetail == null) {   if (other.attriDetail != null)    return false;  } else if (!attriDetail.equals(other.attriDetail))   return false;  if (categoryCode == null) {   if (other.categoryCode != null)    return false;  } else if (!categoryCode.equals(other.categoryCode))   return false;  if (id == null) {   if (other.id != null)    return false;  } else if (!id.equals(other.id))   return false;  if (memberId == null) {   if (other.memberId != null)    return false;  } else if (!memberId.equals(other.memberId))   return false;  if (name == null) {   if (other.name != null)    return false;  } else if (!name.equals(other.name))   return false;  if (pic == null) {   if (other.pic != null)    return false;  } else if (!pic.equals(other.pic))   return false;  if (siteId != other.siteId)   return false;  if (summary == null) {   if (other.summary != null)    return false;  } else if (!summary.equals(other.summary))   return false;  return true; } @Override public String toString() {  return "Information [id=" + id + ", siteId=" + siteId + ", memberId="    + memberId + ", categoryCode=" + categoryCode + ", addTime="    + addTime + ", pic=" + pic + ", summary=" + summary + ", name="    + name + ", attriDetail=" + attriDetail + "]"; } }2.import java.util.List;public class Demo2{ private long group; private String name; private int sort; private List
 attribute;.....setter and getter  }3.import java.util.List;public class Demo3{ private Long id; private String name; private String unit; private int typeid; private boolean is_full; private int sort; private List
 detail; private String userdata; .....setter and getter  }4. public class Demo4{ private Long id; private String detail; private int sort; private String checked;   .....setter and getter   }/*import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import java.io.File;import java.io.IOException;import java.io.PrintStream;import java.net.URL;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.commons.io.FileUtils;public class JSONUtil{public static Object parseJson2ResultBean2(String json, Class type) {  try {   List list = parseJson2ResultList(json, type);   if ((list == null) || (list.isEmpty())) {    return null;   }   return list.get(0);  } catch (Exception e) {   throw new JsonParseException(e);  } }public static List parseJson2ResultList(String json, Class type) {  Map map = json2Map(json);  if ((map == null) || (map.isEmpty())) {   throw new JsonParseException("Json转换失败" + json);  }  if (map.containsKey("param:"))   throw new DubboException("调用接口失败:" + map.get("param:"));  if (map.containsKey("exception:"))   throw new DubboException("调用接口失败:" + map.get("exception:"));  if (!(map.containsKey("jsonValue"))) {   throw new JsonParseException("Json转换失败,Json串中不包含jsonValue:" + json);  }  JSONArray jsonArray = (JSONArray) map.get("jsonValue");  List result = json2ListBean(jsonArray, type);  return result; }  public static Map json2Map(String json) throws JsonParseException {  return ((Map) json2Object(json, Map.class)); }public static Object json2Object(String json, Class type)   throws JsonParseException {  if ((json == null) || (json.trim().length() < 1))   return null;  try {   return JSON.parseObject(json, type);  } catch (Exception e) {   throw new JsonParseException("Json转换失败:" + json, e);  } }public static List json2ListBean(JSONArray jsonArray, Class type) {  if ((jsonArray == null) || (type == null))   return null;  try {   List list = new ArrayList();   Iterator iterator = jsonArray.iterator();   while (iterator.hasNext()) {    JSONObject jsonObject = (JSONObject) iterator.next();    Object obj = JSON.toJavaObject(jsonObject, type);    list.add(obj);   }   return list;  } catch (Exception e) {   throw new JsonParseException(     "Json转换失败:" + jsonArray.toJSONString(), e);  } }  }*/Demo1 demo1 = (Demo1)JSONUtil.parseJson2ResultBean2(json,Demo1.class);String attr_detail = demo1.getAttriDetail();JSONArray jsonarray = JSONArray.fromObject(attr_detail);List
 listone = (List
)JSONArray.toCollection(jsonarray,Demo2.class);demo1.setAttriDetails(listone);return demo1;

创建完的demo对象:

Demo1 demo;

Map map  =  new HashMap();

map.put("demo",demo);

 

html 页面调用:

<#list demo.attriDetails as list>

    <#list list.attribute as attr>

          <tr>

         <td align="right" class="hui hui_lf">${attr.name}:</td>
         <td class="hui bai_rig">

        <#list attr.detail as detail>

                <#if detail.checked == 'checked'>

          ${detail.detail}
            </#if>

        </#list>

             ${attr.userdata}

    </#list>

</#list>

</td>

</tr>

   这样 就可以 调出 你想要的任何数据了。完工。希望有更好的方法 可以 指教。

转载于:https://my.oschina.net/zhangguoqing/blog/414482

你可能感兴趣的文章
Git 大杂烩(初学git 常用的命令)
查看>>
清除默认样式
查看>>
padding-left: 20%;position: relative; left: 50%;
查看>>
zabbix专题:第七章 添加图像Graphs,添加聚合图形Screens
查看>>
移动端随记:flexbox兼容处理
查看>>
CCIE集训营培训路由与交换的全套视频
查看>>
IOS,图片切换动画
查看>>
对象属性合并
查看>>
一步步开发OS(三)任务调度
查看>>
Linux 远程登录
查看>>
Task类使用总结
查看>>
防表单重复提交
查看>>
个人网站优化的出路和方向
查看>>
dhcp的基本配置命令
查看>>
myeclipse添加数据库连接
查看>>
我的友情链接
查看>>
Oracle数据库创建与安装_超越OCP精通Oracle视频教程培训02~04
查看>>
Windows 8 X64 RTM 英文版 9200.16384 正版下载
查看>>
Hyper-V File Server 文件共享服务器
查看>>
WordPress内外网访问的问题解决
查看>>