json 转成java bean_复杂json格式转化为javabean

本文介绍了如何利用阿里巴巴的Fastjson库将包含两层结构的JSON数据转换为Java Bean。通过创建对应的内层和外层Java类,并展示了一个验证类的示例,演示了将JSON字符串解析为Query对象列表的过程。

工具阿里巴巴的fastjson包

com.alibaba

fastjson

1.2.47

场景: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 ;

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 getColumn() {

return column;

}

public void setColumn(List column) {

this.column = column;

}

@Override

public String toString() {

return "Query [id=" + id + ", key=" + key + ", tableName=" + tableName + ", className=" + className

+ ", columns=" + column + "]";

}

}

验证类

packagecom.imply.json;importjava.lang.reflect.Field;importjava.lang.reflect.Modifier;importjava.util.List;importjava.util.Map;importcom.alibaba.fastjson.JSON;public classTdef {public static voidmain(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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值