工具阿里巴巴的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. 在linux 安装python

    wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz tar -zxvf Python-3.7.1.tgz cd Python-3 ...

  2. 线性素数筛(欧拉筛)(超级好的MuBan)

    Problem:找出小于等于n的所有素数的个数. #include <bits/stdc++.h> using namespace std; const int maxn = 1e6; i ...

  3. vue子路由设置、全局组件、局部组件的原生写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. AcWing:167. 木棒(dfs + 剪枝)

    乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位. 然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度. 请你设计一个程序,帮助乔 ...

  5. ast.literal_eval(转)

    eval函数在Python中做数据类型的转换还是很有用的.它的作用就是把数据还原成它本身或者是能够转化成的数据类型.那么eval和ast.literal_val()的区别是什么呢?本文将大家介绍关于P ...

  6. IntelliJ IDEA工具增加test测试方法,报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误

    是因为我在IntelliJ IDEA中,通过plugins增加 插件的时候,在 增加的测试类是junit4.12,改版本的jar包不再包含hamcrest-library.jar .我是通过将自己的项 ...

  7. Nginx事件管理之核心模块ngx_events_module

    1. ngx_events_module核心模块的功能介绍 ngx_events_module 模式是一个核心模块,它的功能如下: 定义新的事件类型 定义每个事件模块都需要实现的ngx_event_m ...

  8. Nexus3.x帐号权限配置

    一.使用admin登录后,点击上方齿轮图标 二.打开左侧导航,Security Privileges:具体的权限明细,比如我创建一个test仓库,这里就会生成 Roles:添加角色,设置每个角色可看到 ...

  9. 腾讯云服务器安装mysql

    首先,我们检测一下系统中是否已安装mysql的相关服务 命令: rpm -qa | grep mysql,无输出则证明未安装 然后我们使用yum检测查找系统自带的mysql安装文件. CentOS7的 ...

  10. SQL Server分页的存储过程写法以及性能比较

    ------创建数据库data_Test ----- create database data_Test GO use data_Test GO create table tb_TestTable - ...