Gson解析数组

被解析的字符串 

[Role {
	accountList = [Account {
		id = 12, username = '谷歌Gson', password = 'null', createtime = null, updatetime = null
	}, Account {
		id = 12, username = '谷歌Gson', password = 'null', createtime = null, updatetime = null
	}], id = 101, roleName = 'admin', createtime = null, updatetime = null
}, Role {
	accountList = [Account {
		id = 12, username = '谷歌Gson', password = 'null', createtime = null, updatetime = null
	}, Account {
		id = 12, username = '谷歌Gson', password = 'null', createtime = null, updatetime = null
	}], id = 102, roleName = 'root', createtime = null, updatetime = null
}, Role {
	accountList = [Account {
		id = 12, username = '谷歌Gson', password = 'null', createtime = null, updatetime = null
	}, Account {
		id = 12, username = '谷歌Gson', password = 'null', createtime = null, updatetime = null
	}], id = 103, roleName = 'user', createtime = null, updatetime = null
}]

数据结构:

Gson解析数组_第1张图片

Gson解析数组_第2张图片

 

解析代码:

        String var = "[{\"accountList\":[{\"id\":12,\"username\":\"谷歌Gson\"},{\"id\":12,\"username\":\"谷歌Gson\"}]" +
                ",\"id\":101,\"roleName\":\"admin\"},{\"accountList\":[{\"id\":12,\"username\":\"谷歌Gson\"}," +
                "{\"id\":12,\"username\":\"谷歌Gson\"}],\"id\":102,\"roleName\":\"root\"},{\"accountList\":" +
                "[{\"id\":12,\"username\":\"谷歌Gson\"},{\"id\":12,\"username\":\"谷歌Gson\"}],\"id\":103,\"roleName\":\"user\"}]\n";

        Role[] roles = gson.fromJson(var,Role[].class);
        System.out.println(Arrays.asList(roles));
        for(Role role : roles){
            System.out.println(role);
        }
        List list1 = gson.fromJson(var,new TypeToken>(){}.getType());
        System.out.println(list1);
        for(Role role : list1){
            System.out.println(role);
        }

结果:

[Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=101, roleName='admin', createtime=null, updatetime=null}, Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=102, roleName='root', createtime=null, updatetime=null}, Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=103, roleName='user', createtime=null, updatetime=null}]
Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=101, roleName='admin', createtime=null, updatetime=null}
Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=102, roleName='root', createtime=null, updatetime=null}
Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=103, roleName='user', createtime=null, updatetime=null}
[Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=101, roleName='admin', createtime=null, updatetime=null}, Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=102, roleName='root', createtime=null, updatetime=null}, Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=103, roleName='user', createtime=null, updatetime=null}]
Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=101, roleName='admin', createtime=null, updatetime=null}
Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=102, roleName='root', createtime=null, updatetime=null}
Role{accountList=[Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}, Account{id=12, username='谷歌Gson', password='null', createtime=null, updatetime=null}], id=103, roleName='user', createtime=null, updatetime=null}

 

你可能感兴趣的:(Gson解析数组)