复杂json格式转化为javabean

Posted rainersha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了复杂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 bao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class Query {

	String id;
	String key;
	String tableName;
	String className;
	List<LinkedHashMap<String, Object>> column;
	List<Column> columns = new ArrayList<Column>();

	public List<LinkedHashMap<String, Object>> getColumn() {
		return column;
	}

	public void setColumn(List<LinkedHashMap<String, Object>> column) {
		this.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> getColumns() {
		return columns;
	}

	public void setColumns(List<Column> columns) {
		this.columns = columns;
	}

	@Override
	public String toString() {
		return "Query [id=" + id + ", key=" + key + ", tableName=" + tableName + ", className=" + className
				+ ", columns=" + columns + "]";
	}

}

  

验证类

package bao;

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);
		queries.stream().forEach(x->{
			x.getColumn().stream().forEach(y->{
				System.out.println(y);
				Column column = (Column) map2Object(y, Column.class); // json里是数组,然后数组里包含object+数组形式,转化之后2层数组里是object形式,需要反射来组装一哈
			    //Column column2 = JSON.parseObject(y.toString(), Column.class);  这个方法并不能转化2层嵌套结构,1层object+数组可以
				if (column != null)
					x.getColumns().add(column);
			});
			
			System.out.println("上为map里内容");
			System.out.println(x.toString());
			System.out.println();
		});
	}
	
	/**
	 * map内容转化为实体对象
	 * @param map
	 * @param clazz
	 * @return
	 */
	public static Object map2Object(Map<String, Object> map, Class<?> clazz){
		if (map == null)
			return null;
		Object obj = null;
		try {
			obj = clazz.newInstance();
			Field[] fields = obj.getClass().getDeclaredFields();
			for (Field field : fields) {
				int mod = field.getModifiers();
				if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
					continue;
				}
				field.setAccessible(true);
				String result = (String) map.get(field.getName());
				if (result != null) {
					if (result.equals("false") || result.equals("true")) {
						field.set(obj, Boolean.parseBoolean(result));
					} else {
						field.set(obj, map.get(field.getName()));
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
}

 

运行结果:

{key=rowIndex, header=序号, width=50, allowSort=false}
{key=id, header=id, hidden=true}
{key=name, header=姓名, width=100, allowSort=true}
上为map里内容
Query [id=user_list, key=id, tableName=用户列表, className=cn.dmego.domain.User, columns=[Column [key=rowIndex, header=序号, width=50, allowSort=false, hidden=false], Column [key=id, header=id, width=null, allowSort=false, hidden=true], Column [key=name, header=姓名, width=100, allowSort=true, hidden=false]]]

 

{key=rowIndex, header=序号, width=50, allowSort=false}
{key=id, header=id, hidden=true}
{key=name, header=名称, width=100, allowSort=true}
上为map里内容
Query [id=role_list, key=id, tableName=角色列表, className=cn.dmego.domain.Role, columns=[Column [key=rowIndex, header=序号, width=50, allowSort=false, hidden=false], Column [key=id, header=id, width=null, allowSort=false, hidden=true], Column [key=name, header=名称, width=100, allowSort=true, hidden=false]]]

 

2019年4月9日 17:11:36

 

以上是关于复杂json格式转化为javabean的主要内容,如果未能解决你的问题,请参考以下文章

复杂的JSON怎么转换为JavaBean

fastjson转化复杂javabean

JSON转化为JAVABEAN集合

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

javabean 与 json 相互转换问题 高手请进!

怎么将json文件转成xml文件