工具阿里巴巴的fastjson包

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

场景:json格式为两层,第一层为数组,第二层object+数组

例:

[
{
"id": "user_list",
"key": "id",
"tableName": "用户列表",
"className": "cn.dmego.domain.User",
"column": [
{
"key": "rowIndex",
"header": "序号",
"width": "50",
"allowSort": "false"
},
{
"key": "id",
"header": "id",
"hidden": "true"
},
{
"key": "name",
"header": "姓名",
"width": "100",
"allowSort": "true"
}
]
},
{
"id": "role_list",
"key": "id",
"tableName": "角色列表",
"className": "cn.dmego.domain.Role",
"column": [
{
"key": "rowIndex",
"header": "序号",
"width": "50",
"allowSort": "false"
},
{
"key": "id",
"header": "id",
"hidden": "true"
},
{
"key": "name",
"header": "名称",
"width": "100",
"allowSort": "true"
}
]
}
] 首先定义javabean,由内而外
内层javabean类
package bao;

public class Column {

	String key;
String header;
String width;
boolean allowSort;
boolean hidden; public String getKey() {
return key;
} public void setKey(String key) {
this.key = key;
} public String getHeader() {
return header;
} public void setHeader(String header) {
this.header = header;
} public String getWidth() {
return width;
} public void setWidth(String width) {
this.width = width;
} public boolean getAllowSort() {
return allowSort;
} public void setAllowSort(boolean allowSort) {
this.allowSort = allowSort;
} public boolean getHidden() {
return hidden;
} public void setHidden(boolean hidden) {
this.hidden = hidden;
} @Override
public String toString() {
return "Column [key=" + key + ", header=" + header + ", width=" + width + ", allowSort=" + allowSort
+ ", hidden=" + hidden + "]";
} }

  

外层javabean类
package com.imply.json;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; public class Query { String id;
String key;
String tableName;
String className;
private List<Column> column ; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getKey() {
return key;
} public void setKey(String key) {
this.key = key;
} public String getTableName() {
return tableName;
} public void setTableName(String tableName) {
this.tableName = tableName;
} public String getClassName() {
return className;
} public void setClassName(String className) {
this.className = className;
} public List<Column> getColumn() {
return column;
} public void setColumn(List<Column> column) {
this.column = column;
} @Override
public String toString() {
return "Query [id=" + id + ", key=" + key + ", tableName=" + tableName + ", className=" + className
+ ", columns=" + column + "]";
} }

  



验证类
package com.imply.json;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map; import com.alibaba.fastjson.JSON; public class Tdef { public static void main(String[] args) {
String str = "[{\"id\":\"user_list\",\"key\":\"id\",\"tableName\":\"用户列表\",\"className\":\"cn.dmego.domain.User\",\"column\":[{\"key\":\"rowIndex\",\"header\":\"序号\",\"width\":\"50\",\"allowSort\":\"false\"},{\"key\":\"id\",\"header\":\"id\",\"hidden\":\"true\"},{\"key\":\"name\",\"header\":\"姓名\",\"width\":\"100\",\"allowSort\":\"true\"}]},{\"id\":\"role_list\",\"key\":\"id\",\"tableName\":\"角色列表\",\"className\":\"cn.dmego.domain.Role\",\"column\":[{\"key\":\"rowIndex\",\"header\":\"序号\",\"width\":\"50\",\"allowSort\":\"false\"},{\"key\":\"id\",\"header\":\"id\",\"hidden\":\"true\"},{\"key\":\"name\",\"header\":\"名称\",\"width\":\"100\",\"allowSort\":\"true\"}]}]";
List<Query> queries = JSON.parseArray(str, Query.class); System.out.println();
queries.stream().forEach(x->{
System.out.print(x.getId());
System.out.print(x.getKey());
System.out.print(x.getTableName());
System.out.print(x.getClassName()); x.getColumn().stream().forEach(y->{
System.out.print(y.getKey());
System.out.print(y.getHeader());
System.out.print(y.getWidth());
});
System.out.println();
}); } }

运行结果:

user_listid用户列表cn.dmego.domain.UserrowIndex序号50ididnullname姓名100
role_listid角色列表cn.dmego.domain.RolerowIndex序号50ididnullname名称100

2019年4月9日 17:11:36

 

最新文章

  1. MyBatis中#,$的用法区别
  2. DP总结
  3. 用LR12录制app,用LR11跑场景,无并发数限制,已试验过,可行!
  4. 表单美化-原生javascript和jQuery下拉列表(兼容IE6)
  5. poj 3625 Building Roads(最小生成树,二维坐标,基础)
  6. obj文件的连接问题以及tlib的基本用法
  7. PHP判断访客是否移动端浏览器访问
  8. 从一个实例,看new FunctionName()的内部机制
  9. CSS3秘笈:第一章
  10. 对java的Thread的理解
  11. scoketserver模块(TCP协议 与 udp 协议)
  12. pygme 安装
  13. Asp.net core 学习笔记 ( Router 路由 )
  14. msp430学习笔记-TA
  15. 关于plantera
  16. C# Lock锁(个人随记)
  17. springboots Helloworld
  18. unable to execute dex:GC overhead limit exceeded unable to execute dex:java heap space 解决方案
  19. Html中的表格
  20. Mockplus设计大赛获奖选手专访 | Intimate:你的专属密友音乐播放器

热门文章

  1. Dapper源码学习
  2. vue-cli3的vue.config.js文件配置,生成dist文件
  3. Spring Boot教程(三十五)使用MongoDB数据库(1)
  4. linux 查看系统性能
  5. shiro环境搭建及基本操作
  6. Hibernate系列1:入门程序
  7. PHP中Smarty的fetch()方法
  8. 本地oracle可以通过localhost连接,无法通过ip地址连接解决方法,oracle远程连接配置
  9. mongoDB 安装过程
  10. Ironic 裸金属管理服务