复杂json格式转化为javabean
工具阿里巴巴的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的更多相关文章
- (转)json格式转换成javaBean对象的方法
把json格式转换成javaBean才可以.于是查了一下资料,网上最多的资料就是下面的这种方式: Java code? 1 2 3 4 5 6 7 8 9 String str = "[{\ ...
- Json格式转化为string格式
今天在学习Jsonp的时候,想通过服务端返回一段json数据,因为使用的服务端是NodeJs,那么就需要在js文件中实现返回json.(这里不懂的同学,可以先了解一下NodeJs的基础概念,在这里,我 ...
- FastJson学习:JSON格式字符串、JSON对象及JavaBean之间的相互转换
当前台需要传送一系列相似数据到后端时,可以考虑将其组装成json数组对象,然后转化为json形式的字符串传输到后台 例如: nodes = $('#PmPbsSelect_tree').tree('g ...
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
fastJson对于json格式字符串的解析主要用到了一下三个类: JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换. JSONObject:fas ...
- 快速将对象转化为JSON格式
1.导入阿里巴巴fastjson包. <!-- fastJson将对象转化为Json对象 --> <dependency> <groupId>com.alibaba ...
- 使用CJSON库实现XML与JSON格式的相互转化
之前完成了一个两个平台对接的项目.由于这两个平台一个是使用json格式的数据,一个是使用xml格式的数据,要实现它们二者的对接就涉及到这两个数据格式的转化,在查阅相关资料的时候发现了这个CJSON库, ...
- JSON(三)——java中对于JSON格式数据的解析之json-lib与jackson
java中对于JSON格式数据的操作,主要是json格式字符串与JavaBean之间的相互转换.java中能够解析JSON格式数据的框架有很多,比如json-lib,jackson,阿里巴巴的fast ...
- 转!!JavaBean,List,Map转成json格式
public class User { private String username; private String password; public String getUsername() { ...
- JavaBean,List,Map转成json格式
public class User { private String username; private String password; public String getUsername() { ...
随机推荐
- jmeter-请求头需要URIEncode编译时
使用函数 ${__urldecode()} 例如 :${__urldecode(%E4%BB%93%E5%BA%93)}${__urldecode(${参数变量})}
- Trying to get property 'art_id' of non-object
“Trying to get property 'art_id' of non-object” 正在尝试获取非对象的“art-id”属性. 我之前也是这么写的没出问题<td>{{$ ...
- 2019牛客暑期多校训练营(第三场)F 单调队列
题意 给一个\(n\times n\)的矩阵,找一个最大的子矩阵使其中最大值与最小值的差小于等于\(m\). 分析 枚举子矩阵的上下边界,同时记录每一列的最大值和最小值. 然后枚举右边界,同时用两个单 ...
- TensorFlow使用记录 (七): BN 层及 Dropout 层的使用
参考:tensorflow中的batch_norm以及tf.control_dependencies和tf.GraphKeys.UPDATE_OPS的探究 1. Batch Normalization ...
- 卸载brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninst ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- 20.Python类型转换,Python数据类型转换函数大全
虽然 Python 是弱类型编程语言,不需要像 Java 或 C 语言那样还要在使用变量前声明变量的类型,但在一些特定场景中,仍然需要用到类型转换. 比如说,我们想通过使用 print() 函数输出信 ...
- HDU 5794 A Simple Chess ——(Lucas + 容斥)
网上找了很多人的博客,都看不太懂,还是大力学长的方法好. 要说明的一点是,因为是比较大的数字的组合数再加上mod比较小,因此用Lucas定理求组合数. 代码如下(有注释): #include < ...
- java jsp基础介绍
1 Jsp基础 1.1 Jsp介绍 JSP(全称Java Server Pages)是一种web动态网页开发技术,通过标签和指令完成用户界面开发和交互操作.它使用J ...
- 【Nginx】 linux环境下安装nginx步骤
开始前,请确认gcc g++开发类库是否装好,默认已经安装. centos平台编译环境使用如下指令 安装make: yum -y install gcc automake autoconf libto ...