Gson解析陣列
阿新 • • 發佈:2018-10-31
被解析的字串
[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}