Gson解析数组

版权声明: https://blog.csdn.net/weixin_40136667/article/details/82708530

被解析的字符串 

[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
}]

数据结构:

解析代码:

        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<Role> list1 = gson.fromJson(var,new TypeToken<List<Role>>(){}.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}

猜你喜欢

转载自blog.csdn.net/weixin_40136667/article/details/82708530