復雜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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM