工具阿里巴巴的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

 

复杂json格式转化为javabean的更多相关文章

  1. (转)json格式转换成javaBean对象的方法

    把json格式转换成javaBean才可以.于是查了一下资料,网上最多的资料就是下面的这种方式: Java code? 1 2 3 4 5 6 7 8 9 String str = "[{\ ...

  2. Json格式转化为string格式

    今天在学习Jsonp的时候,想通过服务端返回一段json数据,因为使用的服务端是NodeJs,那么就需要在js文件中实现返回json.(这里不懂的同学,可以先了解一下NodeJs的基础概念,在这里,我 ...

  3. FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换

    当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台 例如: nodes = $('#PmPbsSelect_tree').tree('g ...

  4. FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换

    fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...

  5. 快速将对象转化为JSON格式

    1.导入阿里巴巴fastjson包. <!-- fastJson将对象转化为Json对象 --> <dependency> <groupId>com.alibaba ...

  6. 使用CJSON库实现XML与JSON格式的相互转化

    之前完成了一个两个平台对接的项目.由于这两个平台一个是使用json格式的数据,一个是使用xml格式的数据,要实现它们二者的对接就涉及到这两个数据格式的转化,在查阅相关资料的时候发现了这个CJSON库, ...

  7. JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson

    java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...

  8. 转!!JavaBean,List,Map转成json格式

    public class User { private String username; private String password; public String getUsername() { ...

  9. JavaBean,List,Map转成json格式

    public class User { private String username; private String password; public String getUsername() { ...

随机推荐

  1. pheonix从入门到进阶

    别人写的帖子感觉很好,记录一下: https://blog.csdn.net/u013411339/article/details/90657429

  2. mybatis insert into 返回id

    <insert id="saveComplaint" useGeneratedKeys="true" parameterType="com.fo ...

  3. js+分布上传大文件

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  4. CentOS8编译openocd-0.10.0

    1.sudo yum install libusbx-devel 2. ./configure --prefix=$HOME/openocd-0.10.0 --enable-jlink CFLAGS= ...

  5. js判断是否是app,及版本号

    判断是否是android,ios,qq,wetchat export const Config = {}; Config.ua = navigator.userAgent.toLowerCase(); ...

  6. linux安装mysql可视化界面

    之前是一直用shell交互界面,但是最近频繁地检查数据库中的数据感觉特别麻烦,便装了一个可视化工具. 安装: $ sudo apt-get install mysql-workbench [sudo] ...

  7. logback条件日志配置

    logback支持条件日志配置,支持在测试环境和正式环境使用不同的参数启用不同的日志配置,从而避免手动修改日志配置文件.项目除了引入logback的包以外,还需要引入构件org.codehaus.ja ...

  8. PHP 练习:租房子

    <form action="text.php" method="post"> 区域:<input type="checkbox&qu ...

  9. oracle中删除某个用户下的所有表

    一般的方法:先使用sql查询: SELECT 'DELETE FROM '|| table_name || ';' FROM USER_TABLES ORDER BY TABLE_NAME; 将查询结 ...

  10. vue问题六:表单验证

    表单验证规则 查看文档:https://blog.csdn.net/weixin_42018790/article/details/80762149 1). el-form增加 :rules=&quo ...